Home > Open Source, WPF > Multithreaded WPF Progress Dialog

Multithreaded WPF Progress Dialog

January 17th, 2008

With this WPF progress dialog, you can display a progress window and invoke a method on a background thread with just a few lines of code. Let’s say, you have a worker method called CountTo100, then all you need to get things running is this:

//create a dialog instance
ProgressDialog dlg = new ProgressDialog();
dlg.Owner = this;
dlg.DialogText = "hello world";

//we cannot access the user interface on the worker thread, but we
//can submit an arbitrary object to the RunWorkerThread method
int startValue = int.Parse(txtStartValue.Text);

//start processing and submit the start value
dlg.RunWorkerThread(startValue, CountTo100);
 

RunWorkerThread displays the dialog as a modal window, and invokes CountTo100 on a background thread. Styling of the dialog is completely up to you, but here’s a screenshot of the dialog running in the sample application:

progressbar-sample

The dialog uses a BackgroundWorker component and supports updating both progress bar and displayed text directly from the worker thread. Cancelling support, returning results from the worker thread and simplified exception handling are available as well. In order to get you started in no time, I’ve created a little sample application that shows a few scenarios, including parameter submission and the handling of exceptions on the worker thread. Enjoy! 🙂

Download Sample (VS2008 / VS2005 projects included): wpfprogressdialog.zip


Author: Categories: Open Source, WPF Tags: , ,
  1. Chris P
    May 11th, 2008 at 14:55 | #1

    Great Stuff! I’ve already incorporated your ideas and its working great! Thanks a lot.

  2. i-developer
    December 16th, 2008 at 12:43 | #2

    That’s what i want. 😉 Sound is perfect.

    Thank you Philipp 🙂

  3. Ramon
    February 23rd, 2009 at 16:38 | #3

    Great work! Thank you so much for sharing.

  4. February 26th, 2009 at 16:14 | #4

    Thanks!
    Works great, in few minutes I get cool looking progress 🙂

  5. ADH
    June 15th, 2009 at 18:57 | #5

    Hi Philipp, thanks so much for your suggestion about using a thread. Great example here. I tried to change your Progress Dialog example to rather update a progressBar on a statusBar of the main form but after much playing around, I’m not sure if this is even possible. Would you be able to point me in the right direction?
    Many thanks!

  6. June 15th, 2009 at 19:42 | #6

    You can invoke a local method that updates your UI from within the worker thread in the sample. Just make sure you don’t invoke it directly, but use Dispatcher.Invoke or Dispatcher.BeginInvoke. Google these methods for further information, and you should be fine 🙂

  7. savitha
    November 3rd, 2009 at 14:48 | #7

    Hi,

    I dont know exactly how it is working.But i tried to implement your code in my application.it is working.but after invoking the background thread again i have to use ui thread.But i get exception saying that the thread it currently owned by some other thread.Please guide me.

  8. November 12th, 2009 at 16:41 | #8

    You would have to be more specific I’m afraid. If you want to update the UI from within the worker thread, you’ll need the Dispatcher (Dispatcher.Invoke) to perform the update.

  9. Edgar
    February 25th, 2011 at 07:16 | #9

    Thanks Philipp, this was exactly what I was looking for.

  10. JB
    April 22nd, 2011 at 13:11 | #10
  1. No trackbacks yet.