Multithreading in WPF: Part II

The BackgroundWorker

We had seen in the previous part that one way to perform asynchronous operation was using the thread class.We create an object of thread class and after supplying it with the method name which contains our asynchronous code we call the thread.start() method. This approach is usefull because we have complete control over the thread object.We can control the threads priority,status(like abort,resume).

However this approach has a drawback since if we access shared data we need to use locking.

In this article we will consider the safest approach to run background task :

The System.ComponentModel.BackgroundWorker class.

We can pass an object to BackgroundWorker.RunWorkerAsync() method which we can access in the DoWork method When the BackgroundWorker begins executing, it grabs a free thread from the CLR thread pool and then fires the DoWork event from this thread. DoWork method contains the logic to perform a time consuming task.

Once the work is complete, the BackgroundWorker fires the RunWorkerCompleted event to notify our application that the work is completed. Here we can access shared data and our user interface, without incurring any problems.

We can not access shared data (such as fields in our window class) or user interface objects in the DoWork eventHandler.In the DoWork eventhandler DoWorkEventArgs object is used for retrieving and returning information. DoWorkEventArgs.Argument is used for accesing the input parameters and DoWorkEventArgs.Result is used for passing the result to the RunWorkerCompleted eventhandler.

In the following example we call the backgroundWorker.RunWorkerAsync method from the button click eventhandler.We declare the backgroundworker variable as a class level variable and retrieve it from the resources collection which we added in the xaml.

    <Window.Resources>
        <cm:BackgroundWorker x:Key="backgroundWorker"
WorkerReportsProgress="True" WorkerSupportsCancellation="True"
DoWork="backgroundWorker_DoWork"
ProgressChanged="backgroundWorker_ProgressChanged"
RunWorkerCompleted="backgroundWorker_RunWorkerCompleted">
        </cm:BackgroundWorker>
    </Window.Resources>

BackgroundWorker.RunWorkerAsync calls the Dowork method in which we retrieve the input.

parameters and after performing the operations sets the "result" property of the DoWorkEventArgs argument which we access in the RunWorkerCompleted eventhandler to retrieve the result.

From the search button click we are calling the backgroundWorker.RunWorkerAsync()method.From the DowWork method we are calling the ProcessTask method in which we simulate a long running task by calling the thread.Sleep .

Supporting Cancellation and Progress

It's just as easy to add support for canceling a long-running task with the BackgroundWorker. The first
step is to set the BackgroundWorker.WorkerSupportsCancellation property to true.

To request a cancellation, your code needs to call the BackgroundWorker.CancelAsync() method. In
this example, the cancellation is requested when a Cancel button is clicked.Here is the code that performs cancelaltion of the task.

   private void btnCancel_Click(object sender, RoutedEventArgs e)
   {
       backgroundWorker.CancelAsync();
   }


The above code itself is not sufficient.

  1. The code that's performing the task needs to explicitly check for the cancel request, perform any required cleanup, and return.
     
  2. The code in our DoWork event handler also needs to explicitly set the DoWorkEventArgs.Cancel property to true to complete the cancellation.

The BackgroundWorker also provides built-in support for tracking progress, which is useful for keeping

the client informed about how much work has been completed in a long-running task.

Following needs to be done for updating the progress of the task.

  1. The DoWork event handling code or the method that implements the logic for our long running task needs to call the BackgroundWorker.ReportProgress() method and provide an estimated percent complete
  2.   
  3. Every time you call ReportProgress(), the BackgroundWorker fires the ProgressChanged event. You can react to this event to read the new progress percentage and update the user interface.

We can update the progress bar by using the following code:

progress.Value = e.ProgressPercentage;

If we execute the attached code we will get the following screen.

MWPF1.gif