Click here to Skip to main content
15,892,575 members
Articles / Desktop Programming / WPF
Tip/Trick

WPF Progress Bar Update Class

Rate me:
Please Sign up or sign in to vote.
4.85/5 (6 votes)
3 Feb 2014CPOL2 min read 20.3K   783   7   2
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)


Written By
Engineer
United States United States
Software Engineer since 1975

Comments and Discussions

 
QuestionHow about databinding? Pin
CzimerA3-Feb-14 21:08
CzimerA3-Feb-14 21:08 
AnswerRe: How about databinding? Pin
RFMeyer4-Feb-14 1:36
professionalRFMeyer4-Feb-14 1:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.