Running on Empty

The few things I know, I like to share.

WPF ProgressBar and Long Running Processes

Introduction

Often during long running processes it is useful to indicate in the UI that the process is running.  Rather than simply locking the UI from the user it is preferable to display a progressbar.  In my current implementation I have a server side process that I know takes a maximum of 30 seconds to complete.  So when I call this process from the UI, I display a progressbar that is timed to reach it’s maximum at 30 seconds.

The ProgressBar Window

<Windown ...>
 

      <ProgressBar x:Name="MessageProgessBar"
                   Maximum="100" Minimum="0"
                   Height="25" Width="275">
        <ProgressBar.Triggers>
          <EventTrigger RoutedEvent="ProgressBar.Loaded">
            <BeginStoryboard>
              <Storyboard>
                <DoubleAnimation Storyboard.TargetName="MessageProgessBar"
                                 Storyboard.TargetProperty="Value"
                                 From="0" To="100" Duration="0:0:30"/>
              </Storyboard>
            </BeginStoryboard>
          </EventTrigger>
        </ProgressBar.Triggers>
      </ProgressBar>
    </StackPanel>
  </Border>
</Window>

Calling Long Running Processes

//Open the Progressbar Window.
ThreadStart ts = delegate
{
 // DO LONG RUNNING PROCESS HERE!
 Dispatcher.BeginInvoke(DispatcherPriority.Normal, (EventHandler)
        delegate
        {
         // Do Cleanup Process HERE!               
        }, null, null);
};
ts.BeginInvoke(null, null);

Thank you Linda for your comments and reminder of subjects I have yet to post.

Please feel free to leave comments or suggestions.

January 23, 2009 Posted by | C#, WPF, XAML | 4 Comments