The RFM_WPFProgressBarUpdate.cs class allows for easy to implement WPF progress bar updating by three methods:
- When the value changes
- On demand
- 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.
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:
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:
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:
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:
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.
- 3rd February, 2014: Initial version