BackgroundWorker Component for Multi Threaded programming in Dote Net 2.0

Hi

The word Multi threaded programming was a deadly thought earlier. It meant some trouble to me for sure. But now in dot net 2.0 multi threaded programming has been made very vary easy.

The new background worker component makes thing very easy. We can use this component for time-consuming processes and the process that can cause the user interface to act very slowly. The background worker runs those on dedicated separate threads. Some of the good example of uses of background worker would be Database transaction, reading large XML file (or any large file), working with web services, working with sockets, file download and upload etc.

The best part of the background worker is the how simple it is to use. We can drag the component form the toolbox to the form (like timer control) or create it programmatically also. There are a few methods that we need to know here. To begin the execution of the background operation being performed we need to use the RunWorkerAsync method.

The RunWorkerSync method fires the DoWork event on a background thread. We code the operation to take place in the handler for this event. If the WorkerSupportsCancellation property is set to true, the CancelAsync method can be called to cancel the background operation at any point of time. When the background operation has either completed, cancelled or has risen on exception, the RunWorkerCompleted event fires.

The backgroundworker component also allows you to show the update progress. To show the progress we need to set WorkerReportsProgress property to true. Now we can call the ReportProgress method while the operation is underway. The report progress event raises the ProgressChanged event. We need to handle this event to keep the user informed about the background progress.

Hope this helps
Thanks
Vikram

Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2009 Vikram Lakhotia