3 回答

TA貢獻1850條經驗 獲得超11個贊
您可以使用 aWaitHandle來保持與工作線程的同步。
private ManualResetEvent _canExit = new ManualResetEvent(true);
private DoBackgroundWork()
{
_canExit.Reset();
backgroundWorker1.RunWorkerAsync(_canExit);
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
// This foreground thread will keep the process alive but allow UI thread to end.
new Thread(()=>
{
_canExit.WaitOne();
_canExit.Dispose();
}).Start();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
ManualResetEvent mre = (ManualResetEvent )e.Argument;
// do your work.
mre.Set();
}
如果您有多個后臺線程要等待,請管理一個WaitHanlde集合并使用它WaitHandle.WaitAll來防止進程退出。
- 3 回答
- 0 關注
- 239 瀏覽
添加回答
舉報