【发布时间】:2016-10-12 14:39:26
【问题描述】:
我正在尝试修补现有 GUI 中的问题,其中大部分已通过粘贴在此答案下面的代码解决 -> How to wait for a BackgroundWorker to cancel?
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?
标签: c# backgroundworker autoresetevent