Click here to Skip to main content
Click here to Skip to main content

Tagged as

BackgroundWorker and ProgressBar demo

, 23 May 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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.
public partial class Form1 : Form
    public Form1()
        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
    // 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
            // Simulate long task
    // 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;


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


About the Author

CEO Dave Meadowcroft
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

QuestionGetting exception System.InvalidOperationException PinprofessionalGovind K24-Aug-14 22:20 
AnswerRe: Getting exception System.InvalidOperationException PinmentorDaveyM6914-Oct-14 4:38 
QuestionIs there any reason...? PinmemberJames McCullough10-Nov-13 16:48 
AnswerRe: Is there any reason...? PinmentorDaveyM6911-Nov-13 2:44 
GeneralRe: Is there any reason...? PinmemberJames McCullough11-Nov-13 9:41 
QuestionBackgroundWorker and ProgressBar Pinmemberrainer_martin25-Jul-13 19:17 
GeneralMy vote of 5 Pinmemberrainer_martin25-Jul-13 19:16 
QuestionVB.Net Code for BackgroundWorker and ProgressBar Demo PinmemberMember 886365516-May-13 11:34 
AnswerRe: VB.Net Code for BackgroundWorker and ProgressBar Demo PinmentorDaveyM6920-May-13 3:45 
GeneralRe: VB.Net Code for BackgroundWorker and ProgressBar Demo PinmemberMember 886365520-May-13 18:59 
GeneralMy vote of 5 PinmemberaGISer24-Apr-13 23:00 
QuestionI am new to Background Worker and Progress Bar Here Pinmemberprashantn.pol2-Jan-13 0:00 
GeneralMy vote of 5 Pinmembertausif_91-Aug-12 4:57 
GeneralMy vote of 4 Pinmembersirama200431-Jul-12 0:01 
GeneralMy vote of 4 Pinmemberprasad kolekar6015-Jun-12 2:26 
GeneralReason for my vote of 5 Simple and to the point. PinmemberProEnggSoft24-Feb-12 5:18 
GeneralReason for my vote of 5 Simple, straightforward, well commen... Pinmember5cw12-Feb-12 12:51 
GeneralThanks a whole lot, I'm a newbe, and after searching thru 30... PinmemberInflicter15-Sep-11 9:48 
GeneralReason for my vote of 5 Simple to the point In Title PinmemberAirelement07414-Jun-11 8:17 
GeneralReason for my vote of 4 practical Pinmemberphiyano2-Aug-10 18:13 

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 | Terms of Use | Mobile
Web03 | 2.8.150414.1 | Last Updated 23 May 2010
Article Copyright 2010 by DaveyM69
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid