Monday, November 8, 2010

Use BackgroundWorker (2)

Some of the advantages to use BackgroundWorker include:
1. We can report the background thread working process.
2. We can cancel the background process.

Now let's look into this.

Let's look into some more memebers of BackgroundWorker class:

Event handler:
ProgressChanged: The event occurs when Reportprogress method is called.

Method:
ReportProgress: The method raises the ProgressChanged event.The method has one int type parameter indicating the percentage of progress.(eg. 0-100);

Property:
WorkerReportsProgress: This property indicates whether the BackgroundWorker componet can report updates. Notice we should set this property to be true to report out progress status.


III. Background Thread Working Status Report


Let me write the sample code below:

using System;
using System.Threading;
using System.ComponentModel;

class Program
{
static BackgroundWorker myWorker = new BackgroundWorker();

static void Main(string[] args)
{
myWorker.DoWork += new DoWorkEventHandler(myWorker_DoWork);
myWorker.RunWorkerAsync();

myWorker.WorkerReportsProgress = true;
myWorker.ProgressChanged += new ProgressChangedEventHandler(myWorker_ProgressChanged);

Thread.Sleep(6000);
}

static void myWorker_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
Console.WriteLine(e.ProgressPercentage.ToString()+"% finished.");
}

static void myWorker_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i <= 10; i++)
{
myWorker.ReportProgress(i * 10);
Thread.Sleep(500);
}
}
}

Notice the logic here, each time the ReportProgress method is called, it fires up the ProgressChanged event and pass the value i*10 to ProgressChangedEventArgs's ProgressPercentage property.

The running results shows like:



The code is at: http://groups.google.com/group/jiezhu0815myfiles/web/BackgroundWorkerProgressExample.rar

IV.Cancel Background Thread


Comparing to Threadpool, Backgroundworker provides easy manipulation to cancel the background thread.

Let's look at the following Memebers:

Method:
CancelAsync: This method request the cancellation of a pending background operation.

Property:

WorkerSupportsCancellation: This property indicates whether BackgroundWorker component support asynchronous cancellation. In order to use the CancelAsync method, this property need to be set to true.

CancellationPending: This property indicates whether the application has been requested to cancel the background thread. In method(long run calculation method) called by DoWork eventhandler, we judge whether this property is true. If this property is true, we set the DoworkEventArg e and set its Cancel property to be true and use keyword "return" to cancel the background working.

I wrote the code sample below:

using System;
using System.Threading;
using System.ComponentModel;

class Program
{
static BackgroundWorker myWorker = new BackgroundWorker();

static void Main(string[] args)
{
myWorker.DoWork += new DoWorkEventHandler(myWorker_DoWork);
myWorker.RunWorkerAsync();

Thread.Sleep(3000);

Console.WriteLine("Cancelling Background working...");
myWorker.WorkerSupportsCancellation = true;
myWorker.CancelAsync();

Console.Read();

}

static void myWorker_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i <= 10; i++)
{
Console.WriteLine(i.ToString());
Thread.Sleep(500);

if (myWorker.CancellationPending == true)
{
e.Cancel = true;
Console.WriteLine("Background working cancelled.");
return;
}
}
}
}

The running result is like below:



The code is attached here:
http://groups.google.com/group/jiezhu0815myfiles/web/CancelBackgroundWorkerExample.rar

No comments: