Using the background worker component
In the previous recipes, we used thread pool to perform long running operations in a different thread. From there, we had to update the UI by marshalling the code to the UI thread, which required additional work.
To overcome this explicit thread pooling and the marshalling of the UI updation on the UI thread, we can use the System.ComponentModel.BackgroundWorker
class. It provides automatic management of long running operations on a background thread.
In this recipe, we will use that BackgroundWorker
to do the asynchronous operations without blocking the UI thread.
Getting ready
We will be using the same example that we have used in previous recipes. You can copy the entire CH10.ThreadingDemo1
project folder and create a new one with the name CH10.ThreadingDemo3
. Launch Visual Studio and open the new project.
How to do it...
Follow these steps to use a background worker, to perform the long running process, and to count the odd and even numbers within a range...