Archive

Archive for March, 2009

Using BackgroundWorker Class

March 19th, 2009

When running a large task on windows from application the application may seems to be un-responsive and some time it looks that the application is in the state of non-responding.

 Start a new Windows Form Application. Drag a button and a Label on the form write the following lines of code in the click event of the button.

for (int i = 0; i < 10; i++)
{
     label1.Text =
” Itreation No “
+ i.ToString();
     System.Threading.
Thread
.Sleep(1000);
 }

Now run the application and u will see that the text on the label is not updating and the application looks like non-responsive.

To overcome this problem the BackgroundWorker class allows you to run an operation on a separate, dedicated thread. Time-consuming operations like downloads and database transactions can cause your user interface (UI) to seem as though it has stopped responding while they are running. When you want a responsive UI and you are faced with long delays associated with such operations, the BackgroundWorker class provides a convenient solution.

To execute a time-consuming operation in the background, create a BackgroundWorker and listen for events (DoWork , ProgressChanged , RunWorkerCompleted )  that report the progress of your operation and signal when your operation is finished. You can create the BackgroundWorker programmatically or you can drag it onto your form from the Components tab of the Toolbox.

 To set up for a background operation, add an event handler for the DoWork event. Call your time-consuming operation in this event handler. To start the operation, call RunWorkerAsync. To receive notifications of progress updates, handle the ProgressChanged event. To receive a notification when the operation is completed, handle the RunWorkerCompleted event.

  //Create a background worker

private System.ComponentModel.BackgroundWorker backgroundWorker1;

Call this method from the constructor of the form to initialize the background worker object.

private void InitializeBackgoundWorker()
{
    //create a new Background Worker
     this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();     
      
     
// backgroundworker can report progress updates.

   this.backgroundWorker1.WorkerReportsProgress = true;
       
     
// Set up the BackgroundWorker object by attaching event handlers. 

     backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
     backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
}

 


private int showMessage(BackgroundWorker worker, DoWorkEventArgs e)
{
for (int i = 0; i < 10; i++)
{
label1.Text = ” Itreation No “ + i.ToString();
System.Threading.Thread.Sleep(1000);
// call the worker object to show the progress.
//This will call the backgroundWorker1_ProgressChanged Method.

worker.ReportProgress(i * 10);
}
return 1;
}

 


private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// Get the BackgroundWorker that raised this event.
BackgroundWorker worker = sender as BackgroundWorker;


// Assign the result to the Result property of the DoWorkEventArgs
// object. This is will be available to the
// RunWorkerCompleted eventhandler.

e.Result = showMessage(worker, e);
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// You can show a progress bar or what ever you want to indicate progress
// This event will fire when you call the ReportProgress method of the Background Worker Object

}

C# , , , , , ,