Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WPF

WPF Progress Bar Update Class

4.85/5 (6 votes)
3 Feb 2014CPOL2 min read 20.6K   786  
Simple way to update WPF progress bar

Introduction

The RFM_WPFProgressBarUpdate.cs class allows for easy to implement WPF progress bar updating by three methods:

  1. When the value changes
  2. On demand
  3. When the timer fires, you specify interval in 10ths of a second.

The Visual Studio 2010 solutions for the test application can be downloaded from the links at the top of this tip.

Background

When I want to use a progress bar in WPF, I do a search on the web on how to update the progress bar. I find that one always needs to use the Dispatcher and Invoke or set up a worker thread. In other programming languages, updating the progress bar was a fairly easy task, but not in WPF, relatively speaking. So, I set out to make it as nearly as easy as it is in other languages.

Using the Code

The class "RFM_WPFProgressBarUpdate" has a long name and is located in the RFM namespace. In the code behind where you will use this class, put the following statement in the "using" section of your code:

C#
using pbu = RFM.RFM_WPFProgressBarUpdate; 

At the location you will want to use this update class, you will add your progress bar to the class and receive a handle like so:

C#
var handle = pbu.New(ProgressBar, min, max, value);  OR 
var handle = pbu.New(ProgressBar, min, max, value, 0); 

Then, where you wish to update the progress bar, usually in a loop, do the following:

C#
pbu.CurValue[handle] = value;  OR 
pub.CurValue[handle] += 1;

That is all there is to it. When the statement above is executed, your progress bar will be updated in the background.

When you have finished using the class to update your progress bar, you will want to remove it from the class like so:

C#
pbu.Remove(handle);

The detailed instruction is in the class's source file. There you will learn how to update on demand or use the timer to update your progress bar.

The attached solution contains a test program showing the three methods of updating the WPF progress bar. Here is a screen shot of that test program:

Points of Interest

You may wish to investigate how the pbu.CurValue[handle] is implemented.

History

  • 3rd February, 2014: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)