我想在现有的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'? –