Take the 2-minute tour ×

I have a GUI application that needs to run long calculations (think a minute or more) and the way that it deals with this is by giving the calculation to a background worker. (this part is fine)

The question I have is if I do something like: this.backgroundWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.doSomethingElse);

is doSomethingElse going to be run on the main UI thread or whatever in the thread pool the background worker ran on?

thank for any help you can provide.

share|improve this question
You could write the System.Threading.Thread.CurrentThread.ManagedThreadId to the output window in the UI, DoWork and WorkCompleted to confirm –  PostMan Nov 18 '10 at 22:42
add comment

3 Answers

up vote 5 down vote accepted

It's going to be run in the same thread that BackgroundWorker is in, ie most usually the UI thread.

share|improve this answer
Ok to the 2nd part, but a Bgw (or any other object) is not 'in a thread'. –  Henk Holterman Nov 18 '10 at 22:44
Sorry, the thread that spawned it would have been a better wording –  konrad Nov 18 '10 at 23:00
This brings up an interesting point actually.. if you use the visual GUI designer in a windows forms application to declare a BackgroundWorker, does the actual BackgroundWorker object end up on the stack for the UI thread or on the heap? –  chuck taylor Nov 19 '10 at 15:57
Afaik it's not really the thread that creates the bgw that matters. It will somehow find the MessagePump through the execution context. Or something like that. –  Henk Holterman Nov 19 '10 at 20:21
add comment

is doSomethingElse going to be run on the main UI thread

Yes, that is the main reason of being for a Backgroundworker. It has 3 events, only DoWork will be executed on a separate (ThreadPool) thread. Completed and ProgressChanged will be marshaled to the 'main' thread.

share|improve this answer
add comment

If the BackgroundWorker was created from the UI thread, then the RunWorkerCompleted event will also be raised on the UI thread.

If it was created from a background thread, the event will be raised on an undefined background thread.

See this post and this connect issue for more information.



share|improve this answer
add comment

Your Answer


