Events raised by BackgroundWorker not executed on expected thread

Posted by Topdown on Stack Overflow See other posts from Stack Overflow or by Topdown
Published on 2010-03-22T06:31:38Z Indexed on 2010/03/22 6:41 UTC
Read the original article Hit count: 269

A winforms dialog is using BackgroundWorker to perform some asynchronous operations with significant success. On occasion, the async process being run by the background worker will need to raise events to the winforms app for user response (a message that asks the user if they wish to cancel), the response of which captured in an CancelEventArgs type of the event.

Being an implementation of threading, I would have expected the RaiseEvent of the worker to fire, and then the worker would continue, hence requiring me to pause the worker until the response is received. Instead however, the worker is held to wait for the code executed by the raise event to complete.

It seems like method I am calling via the event call is actually on the worker thread used by the background worker, and I am surprised, since I expected to see it on the Main Thread which is where the mainform is running. Also surprisingly, there are no cross thread exceptions thrown.

Can somebody please explain why this is not as I expect?

© Stack Overflow or respective owner

Related posts about backgroundworker

Related posts about threading