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

BackgroundWorker and ProgressBar demo

By , 23 May 2010
 
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()
    {
        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)

About the Author

DaveyM69
United Kingdom United Kingdom
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionVB.Net Code for BackgroundWorker and ProgressBar DemomemberMember 886365516 May '13 - 10:34 
Many thanks for your C# code, i have converted this code to VB.Net and it is giving me problems. Can you kindly provide me with the VB.Net code of this demo?
 
Regards
AnswerRe: VB.Net Code for BackgroundWorker and ProgressBar DemomentorDaveyM6920 May '13 - 2:45 
I don't do VB but it's pretty much the same. I've tested this and it seems OK. Just drop a BackgroundWorker (BackgroundWorker1) and a ProgressBar (ProgressBar1) onto Form1 - The code in the form will need to look like this:
 
Public Class Form1
    Public Sub New()
 
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        AddHandler Shown, AddressOf Form1_Shown
        BackgroundWorker1.WorkerReportsProgress = True
        AddHandler BackgroundWorker1.DoWork, AddressOf BackgroundWorker1_DoWork
        AddHandler BackgroundWorker1.ProgressChanged, AddressOf BackgroundWorker1_ProgressChanged
    End Sub
 
    Private Sub Form1_Shown(ByVal sender As Object, ByVal e As EventArgs)
        BackgroundWorker1.RunWorkerAsync()
    End Sub
 
    Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
        For i As Integer = 0 To 100
            BackgroundWorker1.ReportProgress(i)
            System.Threading.Thread.Sleep(100)
        Next
    End Sub
 
    Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs)
        ProgressBar1.Value = e.ProgressPercentage
    End Sub
End Class
Dave

Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.
Astonish us. Be exceptional. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)



GeneralRe: VB.Net Code for BackgroundWorker and ProgressBar DemomemberMember 886365520 May '13 - 17:59 
Many thanks Dave, most appreciated. Smile | :)
GeneralMy vote of 5memberaGISer24 Apr '13 - 22:00 
Useful and simple! Thanks ,you got my 5.
QuestionI am new to Background Worker and Progress Bar Herememberprashantn.pol1 Jan '13 - 23:00 
Your code is very simple i want to use the same i dont understand where do i put my code which is actually doing the work for eg
if have
conn.open();
 
string="select * from employees";
sqlcommand cmd=new sqlcommand(query,conn);
datatable dt=new datatable();
sqldataadapter sda= new sda();
sda.fill(dt);
gridview.datasource=dt;
 
Now ausmming this is very long process taking long time and here i want to show progress bar so that my project is not hanged.
 
Thanks for your help
GeneralMy vote of 5membertausif_91 Aug '12 - 3:57 
simple! Smile | :)
GeneralMy vote of 4membersirama200430 Jul '12 - 23:01 
Nice
GeneralMy vote of 4memberprasad kolekar6015 Jun '12 - 1:26 
good demo
GeneralReason for my vote of 5 Simple and to the point.memberProEnggSoft24 Feb '12 - 4:18 
Reason for my vote of 5
Simple and to the point.
GeneralReason for my vote of 5 Simple, straightforward, well commen...member5cw12 Feb '12 - 11:51 
Reason for my vote of 5
Simple, straightforward, well commented, and works. Nice job DaveyM69.

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 23 May 2010
Article Copyright 2010 by DaveyM69
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid