Alex Rivera | Logout

How do I display progress during a busy loop?

Asked 2009-07-28T14:47:14.580
10

I have a loop that reads plenty of data from an external source. The process takes about 20 seconds, and I want to show the progress to the user. I don't need any fancy progress bars, so I chose to plot my progress in a label that will say "Step 1/1000", then change to "Step 2/1000" etc.

My code looks something like this:

// "count" is the number of steps in the loop, 
// I receive it in previous code

String countLabel = "/"+count.ToString();

for (i = 0; i < count; i++)
{
    ... do analysis ...
    labelProgress.Content = "Step "+i.ToString()+countLabel
}

However, during that analysis the screen is "stuck" and the progress does not show as advancing. I understand this behavior from my past in C++, where I would probably have a separate thread showing a progress bar receiving notifications from the loop, or some form of repaint/refresh, or forcing the window/app to process its message queue.

What's the right way to do it in C#? I'm not tied to the label, so if there's a simple progress-bar popup screen I could use instead of this label it would also be great...

Thanks

Edit
Report

1 Answer

2

The UI is not updated due to the fact that your current thread has a higher priority than your UI thread which will ultimately set the label ;). So until your thread has finished to your stuff it will update your label in the end.

Lucky for us, there is a Dispatcher property on every WPF control that lets you fire up a new thread with another priority..

labelProgress.Dispatcher.Invoke(DispatcherPriority.Background,
                    () => labelProgress.Content = string.Format("Step {0}{1}", i, countLabel));

This fire up a thread in the background and would get the job done! You can also try other DispatcherPriority options

PS I also took the liberty to add an anonymous method and fix your string parsing somewhat.. hope you don't mind..

answered 2009-07-28T14:55:13.990

Your Answer