tags:

views:

460

answers:

1

Hello

I am trying to use the WPF progressbar control with the IsIndeterminate property set to true. The problem I have, is that it doesn't get updated.

I am doing something like this:

pbProgressBar.Visibility = Visibility.Visible; 
//do time consuming stuff
pbProgressBar.Visibility = Visibility.Hidden;

I tried to wrap this in a thread and then dispatch it with the Dispatcher object. How should I solve this problem :).

+2  A: 

You must do the time consuming stuff on a background thread, and you must ensure that the Visibility isn't set back to Hidden until after the background thread has done its thing. The basic process is as follows:

private void _button_Click(object sender, RoutedEventArgs e)
{
   _progressBar.Visibility = Visibility.Visible;

   new Thread((ThreadStart) delegate
   {
       //do time-consuming work here

       //then dispatch back to the UI thread to update the progress bar
       Dispatcher.Invoke((ThreadStart) delegate
       {
           _progressBar.Visibility = Visibility.Hidden;
       });

   }).Start();
}

HTH, Kent

Kent Boogaart
Do I need to kill the thread ?
No, once it exits it will be cleaned up. That is one of quite a few ways to run a task in the background. Take a look at ThreadPool as well.
Kent Boogaart
Actually, BackgroundWorker is better suited if you've got a one-off task like this, because it will give you the OnCompleted notification for free, and it'll be on the UI thread.Also in your example, there's no need to block the worker thread by calling Invoke, BeginInvoke will do just fine
Paul Betts