Click here to Skip to main content
Click here to Skip to main content
Go to top

Basic Backgroundworker

, 11 Feb 2010
Rate this:
Please Sign up or sign in to vote.
Demonstrate how to use backgroundworker

Introduction

Sometimes you just want a simple background thread that does the work and when it is completed, it will do something with the result. Microsoft has a backgroundworker component that works like a thread on a Windows Form. I wanted to demonstrate how to use the BackgroundWorker component.

Background

BackgroundWorker has three events:

  • DoWork - This will be the method that will handle the work when backgroundworker is invoked to run.
  • ProgressChanged - This is for reporting to a progress bar.
  • RunWorkerCompleted - Put code here for the return results of DoWork.

Using the Code

Setting up the Form.

Here I have:

  • 2 TextBoxes
  • 1 Label
  • 1 Button
  • 1 Progress Bar
  • 1 backgroundWorker (under Component Section on ToolBox)

The backgroundworker property WorkerReportProgress needs to be true to report progress:

How does the backgroundworker component run?

private void btnCalculate_Click(object sender, EventArgs e)
{
  MyStruct myStruct = new MyStruct(int.Parse(txtX.Text),int.Parse(txtY.Text));
  backgroundWorker1.RunWorkerAsync(myStruct);
  btnCalculate.Enabled = false;
}    

When button calculate is pressed, it will call the backgroundworker.RunWorkerAsync(object). RunWorkerAsync will trigger the event _DoWork.

In the backgroundWorker1_DoWork event is where you will put your code to do something.

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
 // Get the Object passed in
 MyStruct my = (MyStruct)e.Argument;
 // Do some work 
 for (int i = 0; i <= 10; i++)
 {
  backgroundWorker1.ReportProgress(i);
  Thread.Sleep(100);
 }
 // return the result 
 e.Result = my.X + my.Y;
}

e.Argument is important because that is how we will unbox the object passed into the backgroundworker component.

In this example will be just sleep and report progress. After a certain time it will calculate the result and return it back to the backgrroundWorker1_RunWorkerCompleted.

As the backgroundworker report progress it will update the _ProgressChanged.

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
   progressBar1.Value = e.ProgressPercentage;
}

Finally when it is completed, it will return the result and we want to display the result so we put the code into the _RunWorkerCompleted.

private void backgroundWorker1_RunWorkerCompleted
	(object sender, RunWorkerCompletedEventArgs e)
{
    lblAnswer.Text = e.Result.ToString();
    btnCalculate.Enabled = true;
}

History

  • 11th February, 2010: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Eveng Thao
Software Developer
United States United States
Self motivated , I loved to program. Started programming ever since the Apple II e with Basic. From there I got my degree in B.S. in Electronic Engineering, Master Degree, and my MCSE certificate. I program C++,Assembly,VB 6.0, VB.Net,C#,.Net Compact Framework for Windows Mobile,Qbasic,SQL,Turbo C,Pascal,Java,HTML,ASP,PHP.

Comments and Discussions

 
Generali am getting error PinmemberSurajMutha2-Mar-11 18:19 
GeneralMy vote of 1 PinmemberPriyank Bolia11-Feb-10 22:39 
GeneralMy vote of 1 PinmemberTonik11-Feb-10 21:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140926.1 | Last Updated 12 Feb 2010
Article Copyright 2010 by Eveng Thao
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid