Wednesday, March 03, 2010

C# tips: Multi-threading with BackgroundWorker Component

Now I found a simpler way of doing multi-threading programming in C#: using the Backgroundworker component.

For it to work properly, you must make sure that "any" interaction with UI (controls on your Windows form) must be done at the "ProgressChanged" event handler of your BackgroundWorker component.

The following is a summary of the things you need to do for using the backgroundworker component:

(1) Add the backgroundworker component to your form
(2) Set the "WorkerReportsProgress" property of the backgroundworker to True
(3) Set the "WorkerSupportsCancellation" property of the backgroundworker to True
(4) Create the event handler of the backgroundworker for "DoWork", "ProgressChanged" and "RunWorkerCompleted"
(5) Put in the statements or procedures to be executed by the backgroundworker inside the event handler of "DoWork". This should be the starting point of the thread-processing. This event will be fired when you execute the statement such as

backgroundWorker1.RunWorkerAsync();

The sample codes can be found here
Reference in MSDN

1 comment:

Anni said...

For Multhithreading - Synchronization Primitives
Visit : http://csharp-multithreading.blogspot.com/

Converting a Physical Linux to Virtual

Hmm ... I have done a lot of work on my Linux Lubuntu 15.10 with PHP and PostgreSQL and a few other things ... it is quite time-consuming to...