HOW TO WORK WITH PROGRESS BAR IN WINFORMS

Anupam Mishra

Total Post:135

Points:949
Posted by  Anupam Mishra
C#  .NET 
C#
 460  View(s)
Ratings:
Rate this:
I want to use progress bar and changed its value when events are running . How to working with progress bar in such situation.
please give me a appropriate example.
thank you in advance.
  1. Anupam Mishra

    Post:135

    Points:949
    Re: How to work with Progress Bar in winforms

    I would like to use a BackgroundWorker. Because we have a loop that large in your WinForm it will block and your app will look like it has hanged.Now we are using BackgroundWorker.ReportProgress() to see how to report progress back to the UI thread and progress bar has been changed.

    using System;
    using System.ComponentModel;
    using System.Threading;
    using System.Windows.Forms;
    namespace WorkerDemo
    {
        public partial class Form1 : Form
        {
            BackgroundWorker BackWorker;
            Semaphore s = new Semaphore(0, 1);
            bool b;
            public Form1()
            {
                InitializeComponent();
                BackWorker = new BackgroundWorker();
                BackWorker.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
                BackWorker.ProgressChanged += new ProgressChangedEventHandler
                 (backgroundWorker1_ProgressChanged);
                BackWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler
                     (backgroundWorker1_RunWorkerCompleted);
                BackWorker.WorkerReportsProgress = true;     BackWorker.WorkerSupportsCancellation = true;
            }
            private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
      //Progress bar is changed when report progress is changed
                progressBar1.Value = e.ProgressPercentage;
                label1.Text = "Processing......" + progressBar1.Value.ToString() + "%";
            }
            private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                if (e.Cancelled)
                {
                    label1.Text = "Task Cancelled.";
     
                }
                else if (e.Error != null)
                {
                    label1.Text = "Error while performing background operation.";
     
                }
                else
                {
                    label1.Text = "Task Completed...";
                }
                button1.Enabled = true;
                button2.Enabled = false;
            }
            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                for (int i = 0; i < 100; i++)
                {
                    Thread.Sleep(100);
                    BackWorker.ReportProgress(i);
                    if (BackWorker.CancellationPending)
                    {
                        e.Cancel = true;
                        BackWorker.ReportProgress(0);
                        return;
                    }
                    else
                    {
                        Thread.Sleep(1000);
                    }
                }
                BackWorker.ReportProgress(100);
            }
            private void button1_Click(object sender, EventArgs e)
            {
     
                button1.Enabled = false;
                button2.Enabled = true;
                BackWorker.RunWorkerAsync();
            }
     
            private void button2_Click(object sender, EventArgs e)
            {
                if (BackWorker.IsBusy)
                {
                    BackWorker.CancelAsync();
                }
            }
     
            private void button3_Click(object sender, EventArgs e)
            {
              
                    s.Release();
                    label1.Text = "Task Resume.";
                 }
        }}

    Output:


    you can also set the property of Progress bar, such like this:

    progressbar1.Minimum=0;

    progressbar1.Minimum=100; etc.


      Modified On Jan-20-2016 02:03:15 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!