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)
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;
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.
Sources:
http://www.albahari.com/threading/part3.aspx http://www.dotneat.net/2009/02/10/BackgroundworkerExample.aspx http://www.codeproject.com/KB/cpp/BackgroundWorker_Threads.aspx
Thanks
Setting
e.Cancel
to true doesn't stop the execution of theBackgroundWorker
, it only indicates that the operation was canceled so that you can check it in theRunWorkerCompleted
event. You need to stop the task by returning from theDoWork
event handler :