Click here to Skip to main content
15,881,882 members
Articles / Desktop Programming / Windows Forms

Basic Backgroundworker

Rate me:
Please Sign up or sign in to vote.
2.00/5 (4 votes)
11 Feb 2010CPOL1 min read 50.6K   2.2K   32   3
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.

Image 1Image 2

Here I have:

  • 2 TextBoxes
  • 1 Label
  • 1 Butt<code>on
  • 1 Progress Bar
  • 1 backgroundWorker (under Component Section on ToolBox)

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

Image 3

How does the backgroundworker component run?

C#
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.

C#
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.

C#
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.

C#
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)


Written By
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 Pin
SurajMutha2-Mar-11 18:19
SurajMutha2-Mar-11 18:19 
can u please help me by telling that mystruct is the class name or namespace.
GeneralMy vote of 1 Pin
Priyank Bolia11-Feb-10 22:39
Priyank Bolia11-Feb-10 22:39 
GeneralMy vote of 1 PinPopular
Tonik11-Feb-10 21:42
Tonik11-Feb-10 21:42 

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

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