This recipe describes another approach to asynchronous programming by example of a BackgroundWorker
component. With the help of this object, we are able to organize our asynchronous code as a set of events and event handlers. You will learn how to use this component for asynchronous programming.
To step into this recipe, you will need Visual Studio 2012. There are no other prerequisites. The source code for this recipe could be found in BookSamples\Chapter3\Recipe7
.
To learn how to use the BackgroundWorker
component, perform the following steps:
Start Visual Studio 2012. Create a new C# Console Application project.
In the
Program.cs
file, add the followingusing
directives:using System; using System.ComponentModel; using System.Threading;
Add the following code snippet below the
Main
method:static void Worker_DoWork(object sender, DoWorkEventArgs e) { Console.WriteLine("DoWork thread pool thread id: {0}",Thread.CurrentThread...