It’s the Easter long weekend here in Perth Western Australia all the shops are closed today, so I am at home catching up with some house work and blogging.
Well in this blog I am going to demonstrate how to create a Progress Dialog Box to show the progress of your process and allowing the user to cancel out of the process.
First what you need to do is create a new Windows Form Application project or download the project from here.
Open the form (Form1) and set the name property to ProgressBarForm. Drop and position the ProgressBar, Label, Button and BackgroundWorker control on to the form. You can layout anyway you like or follow the layout that is displayed above. Make sure the control are given the following names in the control name property:
- ProgressBar = progressBar1
- Label = statusLabel
- Button = cancelButton
- BackgroundWorker = backgroundWorker1
Now view the source code of this form and add the System.Threading name space. We will need to call Thread.Sleep() static method later in the code.
To demonstrate a dummy process I decided to perform a count in a for loop, so we need to declare a maximum number to count to. Add a private integer variable called _maxNum.
In the form’s constructor we need to initialise the following:
In the constructor we need to allow the BackgroundWorker to allow for cancellation and update of the process by setting WorkerReportsProgress and WorkerSupportsCancellation property to True.
Now we need to create the following events for the BackgroundWorker as shown below.
First I am going to add come code in the DoWork event to perform a process. A For Loop to iteration from 0 to _maxNum which is declared as 200, in the For Loop scope I have put the thread to sleep for 2 seconds before continuing to the next line of code. Report the current progress by passing the percentage of the process as an Integer value, you need to apply some mathematic skills here to work the percentage value.
The ReportProcess method will invoke the ProgressChanged event as well. Then check for any cancellation pending by calling the CancellationPending method, if the current process has been cancelled then I set the e.Cancel to True to break out of the DoWork event.
The cancelButton Click event I have added code to check if the BackgroundWorker is busy, then call the CancelAsync() method to cancel the process. If the BackgroundWorker is not busy then the CancelAsyn() will not get executed.
The BackgroundWorker ProgressChanged event, when raised the ProgressChangedEventArgs object contains a ProgressPercentage property that holds the current percentage value which was passed through by ReportProgress() method in the DoWork() event. I use this value to set my ProgressBar.Value property and output the value on the status label.
When the BackgroundWorker is complete or cancelled the RunWorkerCompleted event will be raised, but you need to determine if the process was cancelled or not by reading the value from the Cancelled property of the RunWorkerCompletedEventArgs object. At the end of this event I close the form.
To start the BackgroundWorker and make the ProgressBar work, I call the RunWorkerAsync() method in the form Shown event.
Well it’s the end of this demonstration and I hope it’s informative and useful. With this demonstration project you could change it to allow the process to resume, so if the user click on cancel it will ask “are you sure?”. If the user says no, then it resumes the process. See if you can do that!
Download Project Files: