需要帮助来停止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'?
。如果您>在DoWork中没有循环,那么它不会工作。 – stuartd
@Stuart - 正如答案中所述,这是正确的。如果没有循环,没有强制关闭GUI线程中的线程的好方法。 –
@ØyvindKnobloch-Bråthen - 请告诉我什么是从GUI线程强制杀死Backgroundworker线程的坏方法。 (DoWork中没有循环) – Zeeshanef