oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Understanding the BackgroundWorker Component

by Wei-Meng Lee

Multithreading is one of the most powerful concepts in programming. Using multithreading, you can break a complex task into multiple threads that execute independently of one another. One particularly good application of multithreading is in tasks that are synchronous in nature, such as web services calls. By default, web services calls are blocking calls--that is, the caller code will not continue until the web service returns a result. Because web services calls are often slow, this could result in sluggish client-side performance unless you take special steps to call the web service asynchronously. Another good use of multithreading is in tasks that takes a long time to process, such as calculating a complex mathematical formula.

By default, your Windows application uses a single thread of execution. You can use multithreading to create additional threads of execution so that each thread can be executed independently. One particular point you need to bear in mind is that Windows controls are not thread-safe. Put simply, it means that you cannot update the properties of a Windows control in a separate thread; only the main thread can update the controls. In this article, I will show you how multithreading has been simplified in Visual Studio 2005 using the BackgroundWorker component.

The New BackgroundWorker Component

In the .NET 2.0 framework, Microsoft has made programming threads simpler by introducing the BackgroundWorker component. To demonstrate how the BackgroundWorker component helps to make your application more responsive, I will build a Windows application in this article that sums up a range of numbers. This example is purposely designed to be simple so that you can adapt it for your own use.

First, start Visual Studio 2005 Beta 2 and create a new Windows application. Populate the default Windows form with the controls shown in Figure 1:

figure 1
Figure 1. Populating the default Windows form

Drag and drop the BackgroundWorker component (from the Components tab in Toolbox). The BackgroundWorker is a component; hence, it appears below the form in the component section (see Figure 2). The BackgroundWorker is a component that executes an operation on a separate thread.

figure 2
Figure 2. Adding the BackgroundWorker component

Right-click on the BackgroundWorker component and select Properties. Set the WorkerReportsProgress and WorkerSupportsCancellation properties to True so that the component can report on the progress of the thread as well as be cancelled halfway through the thread (see Figure 3).

figure 3
Figure 3. Setting the properties of the BackgroundWorker

Here is how it works. Basically, the user enters a number in the TextBox control (txtNum) and clicks the Start button. The application will then sum up all of the numbers from 0 to that number. The progress bar at the bottom of the page will display the progress of the summation. The speed in which the progress bar updates is dependent upon the number entered. For small numbers, the progress bar will fill up very quickly. To really see the effect of how summation works in a background thread, you should try a large number and watch the progress bar update. You will notice that the window is still responsive while the summation is underway. To abort the process, you can click on the Cancel button. Once the summation is done, the result will be printed on the Label control (lblResult).

Programming .NET Components

Related Reading

Programming .NET Components
Design and Build .NET Applications Using Component-Oriented Programming
By Juval Löwy

Pages: 1, 2

Next Pagearrow