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.
- public DoWorkEventHandler SET_WORKER_METHOD
- {
- set
- {
- bw = new BackgroundWorker();
- bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
- bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
- bw.WorkerReportsProgress = true;
- bw.WorkerSupportsCancellation = true;
- bw.DoWork += value;
- _AsyncMethod = value;
- SetButtonState();
- }
- }
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;".
- public void START_WORK()
- {
- if (_AsyncMethod == null)
- {
- MessageBox.Show(this,"No background task assigned. Task Complete!","No Task!");
- return;
- }
- this._UserCanceled = false;
- this._FinalResult = null;
- this._Message_Cummulative += this._Message = "Process Started\n";
- bw.RunWorkerAsync();
- _HasRunOnce = true;
- SetButtonState();
- this.timer1.Enabled = true;
- }
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.
- private void timer1_Tick(object sender, EventArgs e)
- {
- this.lblMessages.Text = _Message;
- if (this.richTextBox1.Text != _Message_Cummulative)
- this.richTextBox1.Text = _Message_Cummulative;
- if (bw != null)
- if (!bw.IsBusy)
- this.timer1.Enabled = false;
- if (_Continuos_progress)
- {
- int newValue = this.progressBar1.Value + 1;
- this.progressBar1.Value = (newValue > 100 ? 0 : newValue);
- }
- else
- this.progressBar1.Value = _ProgValue;
- }
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
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void butTestBW_Click(object sender, EventArgs e)
- {
- FormProgressViewer fpv = new FormProgressViewer();
-
- fpv.Text = "SOME TEXT";
- fpv.RUN_ONLY_ONCE = false;
- fpv.CONTINOUS_PROGRESS = true;
-
- fpv.SET_WORKER_METHOD = new DoWorkEventHandler(bw_DoWork);
- fpv.START_WORK();
-
- fpv.ShowDialog();
- }
-
-
-
- void bw_DoWork(object sender, DoWorkEventArgs e)
- {
- BackgroundWorker bw = (BackgroundWorker) sender;
- for (int i = 0; i < int.MaxValue/2; i++)
- {
- if (bw.WorkerSupportsCancellation)
- if (bw.CancellationPending)
- {
- i = int.MaxValue / 2 + 1;
- bw.ReportProgress(50, "User Canceled");
-
- e.Result = 60;
-
- }
- if (i == 99)
- 1.ToString();
- if (i % 100 == 0)
- {
- bw.ReportProgress((new Random(i)).Next(100) , "" + i + ": Message Sent: MaxValue = " + int.MaxValue);
- }
- System.Threading.Thread.Sleep(10);
- }
- }
- }
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.