我想在現有的GUI修補問題,其中大部分是由這個答案粘貼下面的代碼解決 - >How to wait for a BackgroundWorker to cancel?當BackgroundWorker在傳遞給DoWork之前完成後,我可以獲取DoWorkEventArgs嗎?
private BackgroundWorker worker = new BackgroundWorker();
private AutoResetEvent _resetEvent = new AutoResetEvent(false);
public Form1()
{
InitializeComponent();
worker.DoWork += worker_DoWork;
}
public void Cancel()
{
worker.CancelAsync();
_resetEvent.WaitOne(); // will block until _resetEvent.Set() call made
// IS THERE ANY WAY TO TELL IF THE BACKGROUNDWORKER STOPPED DUE TO e.Cancel here???
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
while(!e.Cancel)
{
// do something
}
_resetEvent.Set(); // signal that worker is done
}
我的問題是添加到的取消結束評論功能。在這一點上有什麼辦法可以知道背景工作者關閉的原因嗎?
'worker.CancellationPending'? –