Click here to Skip to main content
13,090,178 members (62,104 online)
Click here to Skip to main content
Add your own
alternative version


32 bookmarked
Posted 11 Feb 2010

Basic Backgroundworker

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


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.


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 Butt<code>on
  • 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));
  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++)
 // 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;


  • 11th February, 2010: Initial post


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


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.

You may also be interested in...

Comments and Discussions

Generali am getting error Pin
SurajMutha2-Mar-11 18:19
memberSurajMutha2-Mar-11 18:19 
GeneralMy vote of 1 Pin
Priyank Bolia11-Feb-10 22:39
memberPriyank Bolia11-Feb-10 22:39 
GeneralMy vote of 1 Pin
Tonik11-Feb-10 21:42
memberTonik11-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170813.1 | Last Updated 12 Feb 2010
Article Copyright 2010 by Eveng Thao
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid