Progress bar in Wpf Backgroundworker
Progress bar in Wpf Backgroundworker : WPF comes with a useful control for displaying progress, called the ProgressBar.It works by setting a minimum and maximum valueand then incrementing a value, which will give a visual indication on how far in the process you currently are.
A Progress bar is typically used in software that includes long-running tasks, so displaying a Progress bar is a good way to provide feedback to the user, so that they do not assume there is a problem.
Incoming search terms
Progress bar with Backgroundworker in Wpf, backgroundworker wpf progressbar, backgroundworker in c#, progress bar in wpf, progress bar in wpf backgroundworker, standard wpf progress bar, how to use progressbar in wpf, WPF Progress Bar Control using Dispatcher Thread, Showing progress while performing a lengthy task in wpf, progressbar with backgroundworker in wpf.
A Progress bar is usually a long, thin strip of screen that fills with colour as the process progresses. Ideally, the amount of the bar that is filled represents the amount of work already completed. In some cases it is not possible to know how much processing is required. In these situation an indeterminate progress bar may be used. This animates to show the user that the software has not crashed but gives an indication of the amount of work remaining to be completed.
Here we have used a ProgressBar Control to create the progress bars. By default, ProgressBars let you display a value within the range from zero to one hundred. To set the current value, in other words to partially or totally fill the bar, we change the Value property.
BackgroundWorker makes threads easy to implement in Windows Forms. Intensive tasks need to be done on another thread so the UI does not freeze. It is necessary to post messages and update the user interface when the task is done. BackgroundWorker class provides an easy way to run time-consuming operations on a background thread. The BackgroundWorker class enables you to check the state of the operation and it lets you cancel the operation.
Ok!! Let’s move on the simple example of Progress bar in Wpf with Backgroundworker.
Here in this example we are creating xml of 1000 records using the Backgroundworker know as threading and in parallel we are showing the progress bar control to show as the process progresses.
Following are the create simple example of Progress bar in Wpf with Backgroundworker
ADD 2 Window Pages
ADD XAML CODE – Popup Window
Title="Popup" Height="300" Width="300" WindowStartupLocation="CenterOwner">
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock Text="Please Wait..." FontSize="35" HorizontalAlignment="Center" VerticalAlignment="Center" />
<ProgressBar Name="prgTest" Width="250" Height="30" HorizontalAlignment="Center" VerticalAlignment="Center" />
See below image for more understanding….
ADD CODE BEHIND CODE – Main Window
/// Interaction logic for MainWindow.xaml
public partial class MainWindow : Window
private BackgroundWorker bw = new BackgroundWorker();
Popup pop = new Popup();
private void bw_DoWork(object sender, DoWorkEventArgs e)
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
//Progress Bar Window close
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
pop.prgTest.Value = e.ProgressPercentage;
public void XMLNodeInsertForThread1()
string FileName = System.IO.Directory.GetCurrentDirectory() + "\\CodeScratcherThread1.xml";
for (int i = 1; i <= 1000; i++)
System.Xml.XmlDocument myDoc = new System.Xml.XmlDocument();
XmlNode node = myDoc.SelectSingleNode("/CodescratcherNode");
XmlElement Elements = default(XmlElement);
Elements = myDoc.CreateElement("Codescratcher");
var _with1 = Elements;
_with1.SetAttribute("CodescratcherOutput", "Codescratcher XML Node" + i);
System.Xml.XmlTextWriter myWriter = new System.Xml.XmlTextWriter(FileName, System.Text.Encoding.UTF8);
myWriter.Formatting = System.Xml.Formatting.Indented;
bw.ReportProgress(i / 10);
private void Window_Loaded(object sender, RoutedEventArgs e)
//Background Worker code///
bw.WorkerReportsProgress = true;
bw.DoWork += bw_DoWork;
bw.ProgressChanged += bw_ProgressChanged;
bw.RunWorkerCompleted += bw_RunWorkerCompleted;
//Progress Bar Window
See below image for more understanding….
Over here there is Background Worker with the events like Do work and RunWorkerCompleted.
RUN PROJECT AND CHECK FINAL OUTPUT