Mark Kram
Mark Kram

Reputation: 5832

One Background Worker Thread and Multiple Progress Bars

I want to add multiple progress bars to a WinForm in order to monitor three potentially long processes that are in a nested loop. I theorized that I should be able to use just one Background worker thread, is the following implementation acceptable:

Here is my Class where the work is being done:

class Class1
    {
        public static void UpdatePB(BackgroundWorker worker)
        {
            for (int i = 1; i <= 10; i++)
            {
                for (int ii = 1; ii <= 10; ii++)
                {
                    for (int iii = 1; iii <= 10; iii++)
                    {
                        Thread.Sleep(10);
                        worker.ReportProgress(iii, "PB3");
                    }
                    Thread.Sleep(10);
                    worker.ReportProgress(ii, "PB2");
                }
                Thread.Sleep(10);
                worker.ReportProgress(i, "PB1");
            }
        }

and here is the backgroundWorker ProgressChanged Event:

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        // Change the value of the ProgressBar to the BackgroundWorker progress.
        switch (e.UserState.ToString())
        {
            case "PB1":
                progressBar1.Value = e.ProgressPercentage;
                this.label1.Text = e.ProgressPercentage.ToString();
                break;
            case "PB2":
                progressBar2.Value = e.ProgressPercentage;
                this.label2.Text = e.ProgressPercentage.ToString();
                break;
            case "PB3":
                progressBar3.Value = e.ProgressPercentage;
                this.label3.Text = e.ProgressPercentage.ToString();
                break;
        }

        Application.DoEvents();
    }

Upvotes: 1

Views: 7671

Answers (2)

Mark Kram
Mark Kram

Reputation: 5832

Ok, after many hours of searching I found this LINK that enabled me to figure out what I was doing wrong. Basically, what I was doing was running the progress bar on a BackgroundWorker Thread while still running the process on the main UI Thread. I realized that this is not what I should be doing so I am now running everything on the BackgroundWorker thread. Additionally, you will see in my code example, I had a requirement to maintain two separate Progress bars and update a richtext box with status information. I was able to this functionality rather easily. Here is my final solution:

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace BackgroundWorkerThreadExample
{
    public partial class Form1 : Form
    {
        public delegate void ProgressUpdatedCallaback(ProgressUpdatedEventArgs progress);
        BackgroundWorker bw = new BackgroundWorker();

        public Form1()
        {
            InitializeComponent();
            bw.WorkerReportsProgress = true;
            bw.WorkerSupportsCancellation = true;
            bw.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork);
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            DatabaseProcessor.ProgressUpdated += new DatabaseProcessor.ProgressUpdatedEvent(ProgressUpdated);
            DatabaseProcessor.GetData();
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            bw.Dispose();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            bw.RunWorkerAsync();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (bw.IsBusy == true)
            {
                bw.CancelAsync();
            }
            bw.Dispose();
        }

        private void ProgressUpdated(ProgressUpdatedEventArgs progressUpdated)
        {
            if (InvokeRequired)
            {
                Invoke(new ProgressUpdatedCallaback(this.UpdateProgress), new object[] { progressUpdated });
            }
            else
            {
                UpdateProgress(progressUpdated);
            }
        }

        private void UpdateProgress(ProgressUpdatedEventArgs args)
        {
            ProgressBar pb = new ProgressBar();
            Label lb = new Label();

            if (args.Message == "")
            {
                if (args.PBNum == 1)
                {
                    pb = progressBar1;
                    lb = label1;
                }
                else if (args.PBNum == 2)
                {
                    pb = progressBar2;
                    lb = label2;
                }

                if (pb.Maximum != args.Total)
                {
                    // initial setup
                    pb.Minimum = 0;
                    pb.Maximum = args.Total;
                    pb.Style = ProgressBarStyle.Continuous;
                }

                pb.Value = args.Processed;

                if (args.Total > 0)
                {
                    double progress = args.Processed / (args.Total * 1.0);
                    lb.Text = progress.ToString("P2");
                }
            }
            else
            {
                this.richTextBox1.Text += args.Message;
                //Goto last line
                this.richTextBox1.SelectionStart = this.richTextBox1.Text.Length;
                this.richTextBox1.ScrollToCaret();
            }
            //Application.DoEvents();
        }
    }

    public static class DatabaseProcessor
    {
        public delegate void ProgressUpdatedEvent(ProgressUpdatedEventArgs progressUpdated);
        public static event ProgressUpdatedEvent ProgressUpdated;

        public static void GetData()
        {
            int total = 126;
            Random randomGenerator = new Random();
            for (int i = 0; i < total; i++)
            {
                // Do some processing here
                double delay = (double)randomGenerator.Next(2) + randomGenerator.NextDouble();
                int sleep = (int)delay * 1000;
                System.Threading.Thread.Sleep(sleep);
                RaiseEvent(1, total, i + 1);
                RaiseEvent(0, 0, 0, string.Format("Processing Item {0} \r\n", i + 1));

                for (int ii = 0; ii < total; ii++)
                {
                    // Do some processing here
                    double delay2 = (double)randomGenerator.Next(2) + randomGenerator.NextDouble();
                    int sleep2 = (int)delay2 * 10;
                    System.Threading.Thread.Sleep(sleep2);
                    RaiseEvent(2, total, ii + 1);
                }
            }
        }

        private static void RaiseEvent(int pbNum, int total, int current, string message = "")
        {
            if (ProgressUpdated != null)
            {
                ProgressUpdatedEventArgs args = new ProgressUpdatedEventArgs(pbNum, total, current, message);
                ProgressUpdated(args);
            }
        }
    }

    public class ProgressUpdatedEventArgs : EventArgs
    {
        public ProgressUpdatedEventArgs(int pbNum, int total, int progress, string message = "")
        {
            this.PBNum = pbNum;
            this.Total = total;
            this.Processed = progress;
            this.Message = message;
        }
        public string Message { get; private set; }
        public int PBNum { get; private set; }

        public int Processed { get; private set; }
        public int Total { get; private set; }
    }
}

Upvotes: 3

usr
usr

Reputation: 171178

Well, you are showing a fake progress bar... If that is what you want, then this general approach is ok. Here are two improvements:

  1. Don't use DoEvents. Instead, just keep the message loop of your UI unblocked. Events bill be processed without delay.
  2. Some people might prefer a solution with one or three timers set at the appropriate interval. I think that would be preferable because you don't need a background thread and they are more precise. That said, I think your approach is readable, maintainable and generally fine in this case.

Upvotes: 0

Related Questions