Background worker simplified

This article looks at the Background Worker Technology and encapsulates it into a simple form that can be used over and over to run your background tasks.

Introduction:

Have you ever tried to get a background worker to display a progressbar.  Every article I have seen written goes on and on about how to implement the background worker but never discusses how to put the whole meal deal together.  That is to get the form to show the actual progress.

See the following examples

http://msdn2.microsoft.com/en-us/library/8xs8549b.aspx

http://www.danielmoth.com/Blog/2004/12/backgroundworker-sample.html

Below is a very good c# corner article

https://www.csharpcorner.com/UploadFile/LivMic/BGWorker07032007000515AM/BGWorker.aspx

But this is all very complicated.  I don't necessarily want to fill a data grid or make a SQL call or calculate Fibonacci numbers. But I do want to do "stuff" from time to time and I don't want to have to rethink the whole threading process over and over.  So I created a simple form that you pass the dowork method to. Then just display the form viola you have a nice progress bar running over and over.

So here is the FormProgressViewer image


 
I added some basic elements. A lable so that the messages passed to the backgroundworker could be displayed. A richtextbox (default not visible but infront of the lable) to hold all of the messages. A toggle button to determine what you want to display (current message or all messages). A start button (not visible during task running) A cancel button (disabled during non running tasks) and an OK button to close form when task is complete.

Here is the FormProgessViewer when task complete or cancled


 
The nuts and bolts of the the form are as such

First you need to assign a method that will do the work. The trick is that this method can be created out side of the form as long has it matches the signature of the DoWorkEventHandler. Here is the property for the FormProgressViewer.

  1. public DoWorkEventHandler SET_WORKER_METHOD  
  2. {  
  3.     set  
  4.    {  
  5.        bw = new BackgroundWorker();  
  6.         bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);  
  7.        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);  
  8.        bw.WorkerReportsProgress = true;  
  9.        bw.WorkerSupportsCancellation = true;  
  10.        bw.DoWork += value;   
  11.        _AsyncMethod = value; // just so I can check to see if it exists  
  12.        SetButtonState();  
  13.    }  
  14. }  
Once this method is set then you call the START_WORK() method. You will notice that the START_WORK() method sets a property of the timer component "this.timer1.Enabled = true;". 
  1. public void START_WORK()  
  2. {  
  3.    if (_AsyncMethod == null)  
  4.    {  
  5.        MessageBox.Show(this,"No background task assigned. Task Complete!","No Task!");   
  6.        return;  
  7.    }  
  8.    this._UserCanceled = false;  
  9.    this._FinalResult = null;  
  10.    this._Message_Cummulative += this._Message = "Process Started\n";  
  11.    bw.RunWorkerAsync();  
  12.    _HasRunOnce = true;  
  13.    SetButtonState();  
  14.    this.timer1.Enabled = true;  
  15. }  
This timer is critical. If you don't have some method of having the form update itself the form just sits there and nothing gets updated.  For some reason changing the values in the bw_ProgressChanged will not update the form So you need a separate event to occur that will update the form.
  1. private void timer1_Tick(object sender, EventArgs e)  
  2. {  
  3.     this.lblMessages.Text = _Message;  
  4.     if (this.richTextBox1.Text != _Message_Cummulative)  
  5.     this.richTextBox1.Text = _Message_Cummulative;  
  6.     if (bw != null)  
  7.     if (!bw.IsBusy)  
  8.     this.timer1.Enabled = false;  
  9.     if (_Continuos_progress)  
  10.     {  
  11.         int newValue = this.progressBar1.Value + 1;  
  12.         this.progressBar1.Value = (newValue > 100 ? 0 : newValue);  
  13.     }  
  14.     else  
  15.     this.progressBar1.Value = _ProgValue;  
  16. }  
You can see the Calling Form is very simple. The button just creates the FormProgressViewer. Assigns the method described on this form and starts the task then displays the FormProgressViewer. Now I have something I can use over and over.

The entire calling Form

  1. public partial class Form1 : Form  
  2. {  
  3.     public Form1()  
  4.     {  
  5.         InitializeComponent();  
  6.     }  
  7.     private void butTestBW_Click(object sender, EventArgs e)  
  8.     {  
  9.         FormProgressViewer fpv = new FormProgressViewer();  
  10.         // Some Optional Parameters  
  11.         fpv.Text = "SOME TEXT"// Sets the dialog Caption  
  12.         fpv.RUN_ONLY_ONCE = false// lets the user cancel and click start over and over  
  13.         fpv.CONTINOUS_PROGRESS = true// Progress reporting does not update the progressbar. The internal timer does so  that the bar just cycles.  
  14.         // Required Parameters  
  15.         fpv.SET_WORKER_METHOD = new DoWorkEventHandler(bw_DoWork);  
  16.         fpv.START_WORK(); // best to set dialog options before you start the working thread.  
  17.         // Have to display the form so that you can see what is going on.  
  18.         fpv.ShowDialog();  
  19.     }  
  20.     /// <summary>  
  21.     /// The simple task I want to complete!!!!!  
  22.     /// </summary>  
  23.     void bw_DoWork(object sender, DoWorkEventArgs e)  
  24.     {  
  25.         BackgroundWorker bw = (BackgroundWorker) sender;  
  26.         for (int i = 0; i < int.MaxValue/2; i++)  
  27.         {  
  28.             if (bw.WorkerSupportsCancellation)  
  29.             if (bw.CancellationPending)  
  30.             {  
  31.                 i = int.MaxValue / 2 + 1;  
  32.                 bw.ReportProgress(50, "User Canceled");  
  33.                 // e.Cancel = true; Cannot read e.Result if e.Cancle is true  
  34.                 e.Result = 60;  
  35.                 //bw.CancellationPending = false;  
  36.             }  
  37.             if (i == 99)  
  38.             1.ToString();  
  39.             if (i % 100 == 0)  
  40.             {  
  41.                 bw.ReportProgress((new Random(i)).Next(100) , "" + i + ": Message Sent: MaxValue = " + int.MaxValue);  
  42.             }  
  43.             System.Threading.Thread.Sleep(10); //Tried this to get form FormProgressViewer to redraw!  
  44.         }  
  45.     }  
  46. }  
A couple of notes I did int.MaxValue/2 because I usually exit the loop by setting i = MaxValue but then what happens is the i++ occurs first and makes a -ve max value. (This has to do with the binary math). The Thread.Sleep is important. This task is so fast that it has the potential to generate messages so quickly that the FormProgressViewer cannot handle all of the messages and will hang.  So this controls this. An improvement could be to control this in FormProgressViewer.

A couple of other points about the source code that is worth looking at but not necessarily related to the major jest of this article. I used a panel to hold the 3 buttons on the form. I used docking for the progressbar, label, and richtextbox. The progressbar label and richtextbox are inside a panel so this allows the docking to hold the layout nicely. Then I used anchors on the panels themselves to manage the layout when the form resizes. Just a good example on how to use some of the layout features provided by .Net. I really don't see many articles on how to control layout of forms with ease.  Probably because that is usually more boring to read then how to make a progresbar spin around.

Thank you for reading this article. Your feedback is truly appreciated.