Monday, May 28, 2007

Background Worker

I 'm going to explain how you can use the C# BackgroundWorker component of System.ComponentModel namespace, in your windows form applications.

BackgroundWorker component gives you a way to run a time-consuming task on a separate thread. Actually it works the same way as the asynchronous delegates, but in asynchronous delegate approach you have to consider some issues about working with your UI elements, because there are running on another thread. In BackgroundWorker marshalling issues are abstracted away with an event-based model.

Now let start working with BackgroundWorker.
First, you need an instance of BackgroundWorker class, no diff you are creating this programmatically or by dragging it onto a form at design time from your Component tab of Toolbox.

Next step is to set the event handlers of you object, and finally you have to call RunWorkerAsync() method.

Whenever you call this method it get a free thread from CLR and fires DoWork event. Now you put your codes (The codes that you want to be executed in another thread) in event handler of DoWork event. If the code completed it will raise an event called RunWorkerCompleted to notify you. It 's important to know that this event is not raised on the new thread instead, it will be raised on main thread of your application.

In many cases you may want to prepare some information (Arguments) for your time-consuming task. So, you can achieve this by passing an object into RunWorkerAsync() method, this object is accessible in your DoWork event as an object with it 's event argument.

The event args of DoWork event is an object of type DoWorkEventArgs.
In this object you have a property called Argument for getting what you have passed in RunWorkerAsync() method. You can use it in your time-consuming task.

Then you may want to have the result of your task in your UI. Again for this purpose you have a property called Result which you can set it in your DoWorkEventArgs. And it will be accessible in your RunWorkerCompletedEventArgs of RunWorkerCompleted event.

BackgroundWorker sample download link:
http://www.tabatabaei.info/csharpsamples/backgroundworker.zip

10 comments:

Anonymous said...

Nice simple, succinct summary that got me going ion 10mins. Thanks!

Anonymous said...

Agree with anonymous completely. Thanks for keeping it simple, the basics were explained clearly and I am now cruising along quite nicely with all my background workers!

Anonymous said...

Simple and Very Informative !

Dara said...
This comment has been removed by the author.
Anonymous said...

I were looking this for several days to solve my problem. Thanks for your post that help me to solve my problem.

Anonymous said...

Good article but no light on errorhandling, without it, it does not sound complete. I am struggling to find a way out when my long processing (on worker thread) errors.

Anonymous said...

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!

Anonymous said...

How to access other UI control from bgworker.please help me to aviod cross thread exception.

Venkatesh Kumar R said...

Good One... Thanks a lot...

Anonymous said...

C# Background Worker UI thread for waiting animation