背景工人阶级取消,取消设置标志位,但不退出 [英] background worker class cancellation, sets cancellation pending flag but doesn't quit
问题描述
我叫
obackgroundworker.CancelAsync();在后台工作目前正在做在另一个线程一些工作,然后用
,而(obackgroundworker.IsBusy ==真)
,来等待它退出应用程序(万一用户之前完成
改变主意,而线程是走加工,我想关闭干净)
I call obackgroundworker.CancelAsync(); on a background worker currently doing some work in another thread and then using while (obackgroundworker.IsBusy == true) to wait for it to finish before quitting the application (in case the user changes their mind while the thread is away processing and I want to close down cleanly)
平注销未决正确设置为true,但线程不退出,在辅助线程我有:
The flat for cancellation pending is set to true correctly but the thread doesn't quit, in the worker thread i have:
backgroundworker obackgroundworker = (backgroundworker)sender;
if (obackgroundworker.cancellationpending == true)
e.cancel = true;
这应该检查是否取消是悬而未决,然后设置取消标志设置为true,和我觉得还导致线程终止居然...?或者是有一些其他的功能,我需要当它检测到取消实际结束从线程中调用?
which should check to see if a cancellation is pending and then set the cancelled flag to true, and i think that also causes the thread to actually terminate...? or is there some other function I need to call from the thread when it detects a cancellation to actually end?
我已经读了很多这正是使用后台工作人员的例子。像上面并没有报告任何问题。
I've read a lot of examples that use background workers exactly like above and don't report any problems.
来源:
的 http://www.albahari.com/threading/part3.aspx
的 http://www.dotneat.net/2009/02/10/BackgroundworkerExample.aspx
http://www.codeproject.com/KB/cpp/BackgroundWorker%5FThreads.aspx
感谢
推荐答案
设置 e.Cancel
来真不停止的执行的BackgroundWorker
,它只是表明该操作被取消,这样你可以检查它在 RunWorkerCompleted
事件。你需要从的DoWork
事件处理程序返回停止任务:
Setting e.Cancel
to true doesn't stop the execution of the BackgroundWorker
, it only indicates that the operation was canceled so that you can check it in the RunWorkerCompleted
event. You need to stop the task by returning from the DoWork
event handler :
BackgroundWorker obackgroundworker = (BackgroundWorker)sender;
if (obackgroundworker.CancellationPending == true)
{
e.Cancel = true;
return;
}
这篇关于背景工人阶级取消,取消设置标志位,但不退出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!