I have a form that starts a thread. Now I want the form to auto-close when this thread terminates.
我有一个启动线程的表单。现在我希望表单在此线程终止时自动关闭。
The only solution I found so far is adding a timer to the form and check if thread is alive on every tick. But I want to know if there is a better way to do that?
到目前为止我找到的唯一解决方案是在表单中添加一个计时器,并检查每个tick上是否存在线程。但我想知道是否有更好的方法可以做到这一点?
Currently my code looks more less like this
目前我的代码看起来更像这样
partial class SyncForm : Form {
Thread tr;
public SyncForm()
{
InitializeComponent();
}
void SyncForm_Load(object sender, EventArgs e)
{
thread = new Thread(new ThreadStart(Synchronize));
thread.IsBackground = true;
thread.Start();
threadTimer.Start();
}
void threadTimer_Tick(object sender, EventArgs e)
{
if (!thread.IsAlive)
{
Close();
}
}
void Synchronize()
{
// code here
}
}
5 个解决方案
#1
The BackgroundWorker class exists for this sort of thread management to save you having to roll your own; it offers a RunWorkerCompleted event which you can just listen for.
BackgroundWorker类存在于此类线程管理中,以节省您必须自己滚动;它提供了一个RunWorkerCompleted事件,您可以只听。
#2
Edit to make it call a helper method so it's cleaner.
编辑使其调用辅助方法,使其更清晰。
thread = new Thread(() => { Synchronize(); OnWorkComplete(); });
...
private void OnWorkComplete()
{
Close();
}
#3
If you have a look at a BackgroundWorker, there is a RunWorkerCompleted event that is called when the worker completes.
如果您查看BackgroundWorker,则会在工作程序完成时调用RunWorkerCompleted事件。
For more info on BackgroundWorkers Click Here
有关BackgroundWorkers的更多信息,请单击此处
Or
You could add a call to a complete function from the Thread once it has finished, and invoke it.
完成后,您可以从Thread添加对完整函数的调用,并调用它。
void Synchronize()
{
//DoWork();
//FinishedWork();
}
void FinishedWork()
{
if (InvokeRequired == true)
{
//Invoke
}
else
{
//Close
}
}
#4
Have a look at delegates, IAsyncResult, BeginInvoke and AsyncCallback
看看代理,IAsyncResult,BeginInvoke和AsyncCallback
#5
At the end of your thread method, you can call Close() using the Invoke() method (because most WinForms methods should be called from the UI thread):
在线程方法结束时,可以使用Invoke()方法调用Close()(因为大多数WinForms方法应该从UI线程调用):
public void Synchronize()
{
Invoke(new MethodInvoker(Close));
}
#1
The BackgroundWorker class exists for this sort of thread management to save you having to roll your own; it offers a RunWorkerCompleted event which you can just listen for.
BackgroundWorker类存在于此类线程管理中,以节省您必须自己滚动;它提供了一个RunWorkerCompleted事件,您可以只听。
#2
Edit to make it call a helper method so it's cleaner.
编辑使其调用辅助方法,使其更清晰。
thread = new Thread(() => { Synchronize(); OnWorkComplete(); });
...
private void OnWorkComplete()
{
Close();
}
#3
If you have a look at a BackgroundWorker, there is a RunWorkerCompleted event that is called when the worker completes.
如果您查看BackgroundWorker,则会在工作程序完成时调用RunWorkerCompleted事件。
For more info on BackgroundWorkers Click Here
有关BackgroundWorkers的更多信息,请单击此处
Or
You could add a call to a complete function from the Thread once it has finished, and invoke it.
完成后,您可以从Thread添加对完整函数的调用,并调用它。
void Synchronize()
{
//DoWork();
//FinishedWork();
}
void FinishedWork()
{
if (InvokeRequired == true)
{
//Invoke
}
else
{
//Close
}
}
#4
Have a look at delegates, IAsyncResult, BeginInvoke and AsyncCallback
看看代理,IAsyncResult,BeginInvoke和AsyncCallback
#5
At the end of your thread method, you can call Close() using the Invoke() method (because most WinForms methods should be called from the UI thread):
在线程方法结束时,可以使用Invoke()方法调用Close()(因为大多数WinForms方法应该从UI线程调用):
public void Synchronize()
{
Invoke(new MethodInvoker(Close));
}