Click here to Skip to main content
15,881,709 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 312.9K   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

 
QuestionProgress bar updating while files are copied Pin
Member 157548221-Sep-22 7:23
Member 157548221-Sep-22 7:23 
QuestionWhat is shown in second line? Pin
Member 1369946127-Feb-18 2:54
Member 1369946127-Feb-18 2:54 
QuestionIf you want to run your long time taking task 100 times... Pin
PJH Smit4-Jan-16 22:41
PJH Smit4-Jan-16 22:41 
QuestionInvalidOperationException Pin
Member 1172314127-May-15 5:11
Member 1172314127-May-15 5:11 
AnswerRe: InvalidOperationException Pin
DaveyM6929-May-15 0:51
professionalDaveyM6929-May-15 0:51 
AnswerRe: InvalidOperationException Pin
Aizaz Hussain6-Dec-17 5:46
Aizaz Hussain6-Dec-17 5:46 
QuestionGetting exception System.InvalidOperationException Pin
Govind K24-Aug-14 21:20
professionalGovind K24-Aug-14 21:20 
AnswerRe: Getting exception System.InvalidOperationException Pin
DaveyM6914-Oct-14 3:38
professionalDaveyM6914-Oct-14 3:38 
AnswerRe: Getting exception System.InvalidOperationException Pin
DaveyM6929-May-15 0:54
professionalDaveyM6929-May-15 0:54 
QuestionIs there any reason...? Pin
James McCullough10-Nov-13 15:48
professionalJames McCullough10-Nov-13 15:48 
AnswerRe: Is there any reason...? Pin
DaveyM6911-Nov-13 1:44
professionalDaveyM6911-Nov-13 1:44 
GeneralRe: Is there any reason...? Pin
James McCullough11-Nov-13 8:41
professionalJames McCullough11-Nov-13 8:41 
QuestionBackgroundWorker and ProgressBar Pin
rAINERmARTIN25-Jul-13 18:17
rAINERmARTIN25-Jul-13 18:17 
GeneralMy vote of 5 Pin
rAINERmARTIN25-Jul-13 18:16
rAINERmARTIN25-Jul-13 18:16 
QuestionVB.Net Code for BackgroundWorker and ProgressBar Demo Pin
Member 886365516-May-13 10:34
Member 886365516-May-13 10:34 
AnswerRe: VB.Net Code for BackgroundWorker and ProgressBar Demo Pin
DaveyM6920-May-13 2:45
professionalDaveyM6920-May-13 2:45 
GeneralRe: VB.Net Code for BackgroundWorker and ProgressBar Demo Pin
Member 886365520-May-13 17:59
Member 886365520-May-13 17:59 
GeneralMy vote of 5 Pin
aGISer24-Apr-13 22:00
aGISer24-Apr-13 22:00 
QuestionI am new to Background Worker and Progress Bar Here Pin
prashantn.pol1-Jan-13 23:00
prashantn.pol1-Jan-13 23:00 
GeneralMy vote of 5 Pin
tausif_91-Aug-12 3:57
tausif_91-Aug-12 3:57 
GeneralMy vote of 4 Pin
Sivaraman Dhamodharan30-Jul-12 23:01
Sivaraman Dhamodharan30-Jul-12 23:01 
GeneralMy vote of 4 Pin
prasad kolekar6015-Jun-12 1:26
prasad kolekar6015-Jun-12 1:26 
GeneralReason for my vote of 5 Simple and to the point. Pin
ProEnggSoft24-Feb-12 4:18
ProEnggSoft24-Feb-12 4:18 
GeneralReason for my vote of 5 Simple, straightforward, well commen... Pin
5cw12-Feb-12 11:51
5cw12-Feb-12 11:51 
GeneralThanks a whole lot, I'm a newbe, and after searching thru 30... Pin
Inflicter15-Sep-11 8:48
Inflicter15-Sep-11 8:48 

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.