Archive

Posts Tagged ‘Background Thread’

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: , ,