Progress Bar, Multithread in WPF using C#

I created a video a while ago that discussed how to implement a progress bar in a windows form application. It used the System.Thread class library and the the BackgroundWorker class. With the release of .NET 4, there is a new. easier way to implement this.


In this example, I have added the progress bar to the status bar and a start button in the WPF XAML code.

   <Button Content="Start" Height="23"
           Width="75" />
   <StatusBar Name="StatusBar1"
              Margin="0,0,0,0" />

To add a progress bar to the status bar, I add the following code to the buttonStart_Click method in the code-behind.

ProgressBar progressBar = new ProgressBar();
progressBar.IsIndeterminate = false;
progressBar.Orientation = Orientation.Horizontal;
progressBar.Width = 419;
progressBar.Height = 20;
Duration duration = new Duration(TimeSpan.FromSeconds((30)));
DoubleAnimation doubleAnimatiion = new DoubleAnimation(200, duration);

I should make a comment about setting the duration. You would want the progress bar to complete in about the same time the code you program runs completes. This is very specific to what you program is doing. One example I can give is perhaps your program will process a number of files in a directory. Calculate that it will take about 2 seconds per file to process. Then multiply that number by about 20 – 30% to give you additional time, just in case some files were big. This could give you a ball park figure of the TimeSpan you need to set.

In order the get a smooth running progress bar, you need to code it to run on a different thread than the thread which is performing your actual work. I do this by using the Task namespace from the Task Parallel Library which was newly released with .NET 4.

Action MainThreadDoWork = new Action(() =>
   //add thread safe code here.
   //Confirm thread will not use GUI thread

The second thread will start my progress bar.

Action ExecuteProgressBar = new Action(() =>
   progress.BeginAnimation(ProgressBar.ValueProperty, doubleAnimatiion);

The third thread will wait for the first thread to complete and then re-enable the start button and hide the progress bar.

Action FinalThreadDoWOrk = new Action(() =>
   buttonStart.IsEnabled = true;

Lastly, I use the Task class to start the threads.

Task MainThreadDoWorkTask =
      Task.Factory.StartNew(() => MainThreadDoWork());
Task ExecuteProgressBarTask = new Task(ExecuteProgressBar);
MainThreadDoWorkTask.ContinueWith(t => FinalThreadDoWOrk(), uiThread);

Even though, it is possible to do the same with a BackgroundWorker thread, this is the new approach provided in .NET 4. It is always a good idea and necessary to stay current.

Download the source

Leave a Comment

Your email address will not be published.