Use a Background Worker in WPF using C#

The Silverlight/WPF 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.

When you use the BackgroundWorker class, you can indicate operation progress, completion, and cancellation in the Silverlight/WPF user interface. For example, you can check whether the background operation is completed or canceled and display a message to the user.

XAML code in WPF for the same is here
  1. <Window x:Class="WpfDigital.BackGround"  
  2.  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.  Title="BackGround" Height="300" Width="300">  
  5.    
  6.     <StackPanel>  
  7.         <Button Content="Thread Start" Width="75" Click="Thread_Click"/>  
  8.         <TextBlock x:Name="tbProgress" TextWrapping="Wrap" />  
  9.         <Button Content="Cencel" Width="75" Click="Cancel_Click"></Button>  
  10.         <Button Content="Other functionality is working" Width="75" Click="Other_Click"></Button>  
  11.     </StackPanel>  
  12.  </Window>  
CS file of XAML 
  1. using System.Windows;  
  2.  using System.ComponentModel;  
  3.  namespace WpfDigital  
  4.  {  
  5.      public partial class BackGround : Window  
  6.      {  
  7.          BackgroundWorker myWorker = new BackgroundWorker();  
  8.          public BackGround()  
  9.          {  
  10.              InitializeComponent();  
  11.              myWorker.WorkerReportsProgress = true;  
  12.              myWorker.WorkerSupportsCancellation = true;  
  13.              myWorker.DoWork += myWorker_DoWork;  
  14.              myWorker.RunWorkerCompleted += myWorker_RunWorkerCompleted;  
  15.              myWorker.ProgressChanged += myWorker_ProgressChanged;  
  16.          }  
  17.          void myWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)  
  18.          {  
  19.              tbProgress.Text = e.ProgressPercentage.ToString() + "%";  
  20.          }  
  21.          void myWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)  
  22.          {  
  23.              if (e.Cancelled)  
  24.              {  
  25.                  tbProgress.Text = "Cancelled";  
  26.              }  
  27.              else if (!(e.Error == null))  
  28.              {  
  29.                  tbProgress.Text = "Error " + e.Error.Message;  
  30.              }  
  31.              else  
  32.              {  
  33.                  tbProgress.Text = "DONE";  
  34.              }  
  35.          }  
  36.          void myWorker_DoWork(object sender, DoWorkEventArgs e)  
  37.          {  
  38.              for (int i = 0; i < 10; i++)  
  39.              {  
  40.                  if (myWorker.CancellationPending)  
  41.                  {  
  42.                      e.Cancel = true;  
  43.                      return;  
  44.                   }  
  45.                  else  
  46.                  {  
  47.                      System.Threading.Thread.Sleep(1000);  
  48.                      myWorker.ReportProgress(i * 10);  
  49.                  }  
  50.              }  
  51.          }  
  52.          private void Thread_Click(object sender, RoutedEventArgs e)  
  53.          {      
  54.              myWorker.RunWorkerAsync();  
  55.           }  
  56.          private void Cancel_Click(object sender, RoutedEventArgs e)  
  57.          {  
  58.              if (myWorker.WorkerSupportsCancellation == true)  
  59.              {  
  60.                  myWorker.CancelAsync();  
  61.              }  
  62.          }  
  63.          private void Other_Click(object sender, RoutedEventArgs e)  
  64.          {  
  65.              System.Windows.Forms.MessageBox.Show("yes its working fine with background");  
  66.          }  
  67.       }  
  68.  }  

Output

Background

Ebook Download
View all
Learn
View all