需要帮助停止BackgroundWorker线程。
我正在尝试停止一个BackgroundWorker线程。以下是我的操作:
在停止按钮点击事件中(UI层):
if (backgroundWorker.IsBusy == true &&
backgroundWorker.WorkerSupportsCancellation == true)
{
backgroundWorker.CancelAsync();
}
在DoWork事件(UI层)中:
if ((backgroundWorker.CancellationPending == true))
{
e.Cancel = true;
}
else
{
//Function which progresses the progress bar is called
//here with its definition in business layer
}
一旦DoWork事件被触发并且我的程序控制在业务层定义的函数中,我该如何返回到DoWork事件以设置 'e.Cancel = true'?
Thread.Abort
方法手动中止它。我自己从未尝试过,希望我永远也不必这样做 ;) - Øyvind Bråthen