ProgressBar, BackgroundWorker Controls in C# for Windows Applications

 

ProgressBar is the C# Windows control used to display the progress of the lengthy operation for windows applications in C#. BackgroundWorker is also C# Windows control used to run the separate thread in the background for the particular operation. 

BackgroundWorker control will run the operation in separate thread apart from the main thread, after completing the operation it will raise the event RunWorkerCompleted event. 

Here we discuss about how to display the progress by using ProgressBar control while some operation is running in the background by using BackGroundWorker control in C#. Create C# Windows application and add ProgressBar, BackGroundWorker and Button controls to the form. 

Set the ProgressBar control properties as shown below.

 

 

Now add the below code for Form1.cs class.

 

using System; 

using System.ComponentModel; 

using System.Windows.Forms;

 

namespace CSharpProgressBar 

{ 

    public partial class Form1 : Form 

    { 

        public Form1() 

        { 

            InitializeComponent(); 

        }

 

        private void Form1_Load(object sender, EventArgs e) 

        { 

            backgroundWorker1.RunWorkerAsync(); 

            progressBar1.Value = 0; 

        }

 

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 

        { 

            int n = 200000;

 

            for (int i = 1; i < n; i++) 

            { 

                backgroundWorker1.ReportProgress(i * 100 / n); 

            } 

        } 

 

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) 

        { 

            progressBar1.Value = e.ProgressPercentage; 

        } 

 

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 

        { 

            MessageBox.Show("Task Completed"); 

            progressBar1.Value = 0;

        }

 

        private void button1_Click(object sender, EventArgs e) 

        { 

            backgroundWorker1.RunWorkerAsync(); 

        } 

    }

}

 

As shown above, we added the some operation for DoWork event backgroundWorker1 control and reporting the progress for the ProgressChanged event backgroundWorker1 control. In the backgroundWorker1 control ProgressChanged event we are mentioning the value for ProgressBar control progressBar1 control. In the backgroundWorker1 control RunWorkerCompleted event, we are resetting the progressbar control value.

 

Right Click on BackGroundWorker conttrol backgroundWorker1 and set its events as shown below.

 

 

 

Set backgroundWorker1 control WorkerReportsProgress property to True as shown below.

 

Set the Button control button1 event as shown below.

 

 

 

If you run the application the output is as shown below.

                                                                                                                                                                                                        

                                                                                             CSharpProgressBar.zip (38.79 kb)