Introduction
The BackgroundWorker allows you to execute operations that take a long period of time such as database transaction or file downloads, asynchronously on a separate thread and allow the user interface to remain responsive.
Start an Operation in the Background
First, you need to add an instance of the BackgroundWorker component to your application, if you use visual studio, just drag it from the toolbox to your application or you can create it manually as follow:
- BackgroundWorker backgroundWorker1 = new BackgroundWorker();
- BackgroundWorker backgroundWorker1 = new BackgroundWorker();
- //start the operation on another thread
- private void btnStart_Click(object sender, EventArgs e)
- {
- backgroundWorker1.RunWokerAsync();
- }
- //DoWork event handler is executed on a separate thread
- private void backgroundWorker1_DoWork(object sender, DoWorkeventArgs e)
- {
- //a long running operation
- Thread.Sleep(5000);
- }
- BackgroundWorker backgroundWorker1 = new BackgroundWorker();
- //start the operation on another thread
- private void btnStart_Click(object sender, EventArgs e)
- {
- backgroundWorker1.RunWokerAsync(2000);
- }
- //DoWork event handler is executed on a separate thread
- private void backgroundWorker1_DoWork(object sender, DoWorkeventArgs e)
- {
- int input = (int)e.Argument;
- //a long running operation
- Thread.Sleep(input);
- }
By using the BackgroundWorker you have the ability to report progress updates of the executing operation. To use this options you must set the WorkerReportsProgress to true.
To start report progress you call ReportProgress() method and use it to pass a parameter that have the value of the percentage of the progress that have been completed. This method raises the BackgroundWorker.ProgressChanged event. In the event handler of the ProgressChanged you can recieve the value of the progress precentage on the main thread using the ProgressPercentage property of the instance of ProgressChangedEventArgs.
- BackgroundWorker backgroundWorker1 = new BackgroundWorker();
- backgroundWorker1.WorkerReportsProgress = true;
- //start the operation on another thread
- private void btnStart_Click(object sender, EventArgs e)
- {
- backgroundWorker1.RunWokerAsync();
- }
- //DoWork event handler is executed on a separate thread
- private void backgroundWorker1_DoWork(object sender, DoWorkeventArgs e)
- {
- //a long running operation
- for (int i = 1; i < 11; i++)
- {
- Thread.Sleep(2000);
- backgroundWorker1.ReportProgress(i*10);
- }
- }
- private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
- {
- progressBar1.value = e.ProgressPercentage;
- }
With the BackgroundWorker you have the ability to cancel the background operation, to do this you first must set WorkerSupportsCancellation property to true.
The next step is to call the CancelAsync() method by doing so you set the CancellationPending property to true, by polling the CancellationPending property you can determine whether or not to cancel the background operation.

Koteswararao MallisettiPosted Oct 19, 2010, 4:36 AM
how can i use thin in a web page suppose i click the button and update the lable text on the same page with out refresh page is it possible with the background workerprocess
privacy 101Posted Oct 11, 2010, 2:22 PM
Amr, Thanks for taking the time to post this example. It is very helpful indeed!
Mohamed AbdullahPosted Jun 17, 2009, 9:26 AM
good example can u give me an example how to use ur example to retrieve records from database and populate a datagridview thanks in advance Mohamad Abdullah