Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which thread does backgroundworker completed event handler run on?

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.

like image 755
chuck taylor Avatar asked Nov 18 '10 22:11

chuck taylor


People also ask

Is BackgroundWorker threaded?

BackgroundWorker, is a component in . NET Framework, that allows executing code as a separate thread and then report progress and completion back to the UI.

What is the difference between BackgroundWorker and thread?

BackgroundWorker has already implemented functionality of reporting progress, completion and cancellation - so you don't need to implement it by yourself. Usage of Thread gives you more control over the async process execution (e.g. thread priority or choosing beetween foreground/background thread type).

Does BackgroundWorker use ThreadPool?

BackgroundWorker Class This class is essentially a wrapper for the ThreadPool class and uses a thread pool in its implementation.

How does BackgroundWorker work in VB net?

BackgroundWorker enables a simple multithreaded architecture for VB.NET programs. This is more reliable, and easier, than trying to handle threads directly. In the Toolbox pane, please double-click on the BackgroundWorker icon. This will add a BackgroundWorker1 instance to the tray at the bottom of the screen.


2 Answers

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

like image 174
Homde Avatar answered Nov 12 '22 19:11

Homde


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.

like image 39
Henk Holterman Avatar answered Nov 12 '22 19:11

Henk Holterman