Click here to Skip to main content
15,890,399 members
Articles / Programming Languages / C#
Tip/Trick

BackgroundWorker and ProgressBar demo

Rate me:
Please Sign up or sign in to vote.
4.70/5 (46 votes)
23 May 2010CPOL 313.2K   48   28
A simple demonstration of how to use a BackgroundWorker with a ProgressBar
This gets asked almost daily so I've written this short demonstration code to demonstrate how to use a System.ComponentModel.BackgroundWorker in combination with a System.Windows.Forms.ProgressBar. All the code is commented so the flow/steps should be easy to understand.

Just drop a ProgressBar (progressBar1) and a BackgroundWorker (backgroundWorker1) onto your form and copy and paste this code to see it in action.
C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        Shown += new EventHandler(Form1_Shown);

        // To report progress from the background worker we need to set this property
        backgroundWorker1.WorkerReportsProgress = true;
        // This event will be raised on the worker thread when the worker starts
        backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
        // This event will be raised when we call ReportProgress
        backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
    }
    void Form1_Shown(object sender, EventArgs e)
    {
        // Start the background worker
        backgroundWorker1.RunWorkerAsync();
    }
    // On worker thread so do our thing!
    void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        // Your background task goes here
        for (int i = 0; i <= 100; i++)
        {
            // Report progress to 'UI' thread
            backgroundWorker1.ReportProgress(i);
            // Simulate long task
            System.Threading.Thread.Sleep(100);
        }
    }
    // Back on the 'UI' thread so we can update the progress bar
    void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        // The progress percentage is a property of e
        progressBar1.Value = e.ProgressPercentage;
    }
}

License

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


Written By
CEO Dave Meadowcroft
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 5 Simple to the point In Title Pin
Airelement07414-Jun-11 7:17
Airelement07414-Jun-11 7:17 
GeneralReason for my vote of 4 practical Pin
phiyano2-Aug-10 17:13
phiyano2-Aug-10 17:13 

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.