WPF progressbar with BackgroundWorker


This article discusses the WPF progressbar with BackgroundWorker.

I hope most of us from a C# (or VB.Net). Here one of the Microsoft's new technology WPF have changed some common concepts like Application.DoEvents() to their own style and this article for newbie's of WPF. This document deals the progressbar with BackgroundWorker threading behaviours.

I have attached the sample application.

This application contains Start/Stop progressbar functionality.

We have to taken care of some events from backgroundworker thread are as follows

1. DoWork – Do our functionality here
2. ProgressChanged – Update progress bar goes here
3. RunWorkerCompleted – Once process completed it will notify us.

Let we discuss the code to look into how we use it.

XAML,


<StackPanel Orientation="Vertical" VerticalAlignment="Center" Margin="0,50,0,0" HorizontalAlignment="Left" Width="Auto" Opacity="1">
<Label Height="26" Margin="10,0,0,0" Name="lblProcessing" HorizontalAlignment="Left" Foreground="Black" Content="Processing..."
HorizontalContentAlignment="Left" Width="Auto"></Label>
<ProgressBar Margin="10" Height="19" Width="256" HorizontalAlignment="Left" Name="pbProcessing"></ProgressBar>
</StackPanel>

<Button Height="24" Name="cmdStart" Width="75" Click="cmdStart_Click" HorizontalAlignment="Left" Margin="57,80,0,0" VerticalAlignment="Top">Start</Button>
<Button Height="24" Margin="0,80,60,0" Name="cmdStop" VerticalAlignment="Top" Click="cmdStop_Click" HorizontalAlignment="Right" Width="75">Stop</Button>


C# code,

We have to declare a module level variable for BackgroundWorker. It will do our work as a separate thread.

System.ComponentModel.BackgroundWorker mWorker;

In start button click event need to handle BackgroundWorker. Initialize their required events here. BackgroundWorker always works as a background thread. Now we should call two BackgroundWorker properties
1. WorkerReportsProgress – It will updates the progressbar when status getting updates. Set value as true, default is false
2. WorkerSupportsCancellation - It will cancel the progressbar when cancellation fired. Set value as true, default is false

In WPF we can call doevents as follows,

System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background,
new System.Threading.ThreadStart(delegate { }));

private void cmdStart_Click(object sender, RoutedEventArgs e)
{
pbProcessing.Value = 0;

mWorker = new System.ComponentModel.BackgroundWorker();
mWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(worker_DoWork);
mWorker.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(worker_ProgressChanged);
mWorker.WorkerReportsProgress = true;
mWorker.WorkerSupportsCancellation = true;
mWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
mWorker.RunWorkerAsync();

while (pbProcessing.Value != 100) //(mWorker.IsBusy)
{
if (!mWorker.CancellationPending)
{
try
{
pbProcessing.Value = (pbProcessing.Value + 0.05) % 100;
}
catch (System.Exception ex)
{
// No action required
}
}
else
{
MessageBox.Show( this,"Process cancelled", "Cancel Process", MessageBoxButton.OK);
break;
}

System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background,
new System.Threading.ThreadStart(delegate { }));
}

}

private void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
// Do your work here, its on seperate thread
}

private void worker_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
pbProcessing.Value = e.ProgressPercentage;
}

private void worker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
// Stop Progressbar updatation
}

private void cmdStop_Click(object sender, RoutedEventArgs e)
{
mWorker.CancelAsync();
}


If you click stop button it will calls BackgroundWorker.CancelAsync(). It stops the process.

For testing purpose I have updated the progress value through while loop

You are all set…


Attachments

  • WPF progressbar with backgroundworker (38099-3076-Progressbar.rar)
  • Related Articles

    More articles: Progress bar Progress bar WPF WPF

    Comments

    Author: manisha06 May 2010 Member Level: Gold   Points : 0

    good work. Thanks for posting.

    -- Manisha Chaubey

    Author: shalini rathore14 Mar 2012 Member Level: Bronze   Points : 0

    good job..
    very helpful concept..
    thanks

    Author: Nagarajan08 Apr 2012 Member Level: Gold   Points : 0

    thanks guys



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: