Multithreading with the .NET Framework
The .NET Framework includes a Thread class, thread pools, and asynchronous processing, all of which support multithreading. Previously, if you needed multithreading you were encouraged to use threads from the thread pools. While this process is relatively easy, the new BackgroundWorker component builds on existing support and makes multithreading even easier to use than in versions of .NET before 2.0.
Before we get started, let me make it clear that the BackgroundWorker component is a visual component that offers design-time support. This design-time makes the component very easy to use, but not any easier to demonstrate in an article format—text is easier to use here. In this article I’ll demonstrate the BackgroundWorker component with lines of code. (Remember, components are just classes, and if you don’t have a Form handy you can use components like any other class.)
To demonstrate how easy it is to begin using the BackgroundWorker component, with or without a Windows Form, all we need to do is add an Import statement for the System.ComponentModel namespace and declare and create an instance of the BackgroundWorker component. The results are shown in the code fragment in Listing 1.
Listing 1 A console application fragment that imports the needed namespace and creates an instance of the BackgroundWorker component.
Imports System.ComponentModel Module Startup Sub Main() Dim worker As BackgroundWorker = New BackgroundWorker() End Sub End Module
Next, we need to give the component some work to do.