[Mono-dev] BackgroundWorker.CancelAsync() behaves incorrectly?

Jordan Callicoat MonkeeSage at gmail.com
Sat Mar 22 01:08:24 EDT 2008


According to MSDN, System.ComponentModel.BackgroundWorker.CancelAsync() is
supposed to set the CancellationPending property to true, to be accessed
from the worker thread.[1][2][3]

In mono 1.2.6 (and current SVN--haven't looked at other versions),
CancelAsync() sets cancel_pending (CancellationPending gets the value of
this ivar) to true, then calls AsyncOperation.PostOperationCompleted(), with
a delegate that is Post()'ed to the SynchronizationContext and eventually
calls the RunWorkerCompleted event handler from OnRunWorkerCompleted; after
this, cancel_pending is set to false and CancelAsync() returns.[4][5][6]

This means that CancellationPending is only true in the RunWorkerCompleted
callback, and further, the RunWorkerCompleted callback is executed *while
the worker thread is still working*...this seems very wrong. According to
the MSDN entries mentioned above, CancelAsync() is only supposed to set
CancellationPending, which the worker thread is supposed to check
periodically to decide whether to continue or terminate. I can't see why
CancelAsync() should do anything more than set CancellationPending to true
and return, and let the RunWorkerCompleted event trigger when the worker
thread actually terminates.

I looked a bit but didn't find anything very helpful on this issue. Is this
behavior a bug or is it intentional? Has this been discussed already?

[1]
http://msdn2.microsoft.com/en-us/library/system.componentmodel.backgroundworker.cancelasync.aspx
[2]
http://msdn2.microsoft.com/en-us/library/system.componentmodel.backgroundworker.cancellationpending.aspx
[3] http://msdn2.microsoft.com/en-us/library/hybbz6ke.aspx
[4]
http://anonsvn.mono-project.com/source/trunk/mcs/class/System/System.ComponentModel/BackgroundWorker.cs
[5]
http://anonsvn.mono-project.com/source/trunk/mcs/class/System/System.ComponentModel/AsyncOperation.cs
[6]
http://anonsvn.mono-project.com/source/trunk/mcs/class/corlib/System.Threading/SynchronizationContext.cs

-- 
View this message in context: http://www.nabble.com/BackgroundWorker.CancelAsync%28%29-behaves-incorrectly--tp16216320p16216320.html
Sent from the Mono - Dev mailing list archive at Nabble.com.



More information about the Mono-devel-list mailing list