---
title: "How to use backgroundWorker Class in C#"  
description: "In this blog I am trying to explain how to use backgroundWorker class in C#.BackgroundworkerThe Backgroundworker class provides a simple approach to r"  
author: "Vijay Shukla"  
published: 2013-06-08  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/523/how-to-use-backgroundworker-class-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 4 minutes  

---

# How to use backgroundWorker Class in C#

In this blog I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to explain how to use backgroundWorker [class in C#](https://www.mindstick.com/forum/160399/how-to-create-a-custom-generic-class-in-c-sharp-provide-an-example).

##### Backgroundworker

The Backgroundworker class provides a simple approach to run time-consuming [operations](https://www.mindstick.com/blog/304985/how-does-devops-bridge-the-gap-between-development-and-operations-teams-like-git) on a [background thread](https://www.mindstick.com/articles/620/foreground-and-background-thread-in-c-sharp-threading). The Backgroundworker class enables you to check the state of the [operation](https://www.mindstick.com/forum/33917/how-to-callback-operation-using-delegate-in-c-sharp) and it lets you cancel the operation.

When using of the Backgroundworker class, you can point to operation [progress](https://yourviews.mindstick.com/view/284/devotees-arrived-in-prayagraj-while-work-is-still-in-progress), completion, and [cancellation](https://answers.mindstick.com/qa/46736/should-you-buy-cancellation-travel-insurance). For example, you can [check whether](https://www.mindstick.com/forum/157543/write-a-java-program-to-check-whether-a-string-is-a-palindrome) the background operation is completed or canceled and display a message to the user.

Hoe to o use the Backgroundworker class: -

1. Create an instance of the Backgroundworker class.

a. Backgroundworker _ backgroundworker = new Backgroundworker();

2. Write the below code where which you want to background operation to allow cancellation and to report progress.

b. _ backgroundworker.WorkerSupportsCancellation = true;

c. _ backgroundworker.WorkerReportsProgress = true;

3. Create an [event handler](https://www.mindstick.com/forum/1371/c-sharp-dot-net-event-handler-delegate-question) for the background worker's DoWork event. This event is raised when you call the RunWorkerAsync method.

```
private void _backgroundworker_DoWork(object sender, DoWorkEventArgs e){}
```

4. Create an event handler for the background worker's ProgressChanged event. In this event add code to indicate the progress. This event is raised when you call the ReportProgress method.

```
private void _backgroundworker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
```

5. Create an event handler for the RunWorkerCompleted event. This event is raised when the DoWork event handler returns.

```
private void _backgroundworker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
 }
```

##### Example: -

```
using System.ComponentModel;using System.Windows;using System.Windows.Controls;
namespace My_BackgroundWorker_CS{
    public partial class Page : UserControl    {
        private BackgroundWorker _backgroundworker = new BackgroundWorker();
        public Page()        {
            InitializeComponent();
            _backgroundworker.WorkerReportsProgress = true;            _backgroundworker.WorkerSupportsCancellation = true;            _backgroundworker.DoWork += new DoWorkEventHandler(bw_DoWork);            _backgroundworker.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
            _backgroundworker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
        }
        private void buttonStart_Click(object sender, RoutedEventArgs e)        {
            if (_backgroundworker.IsBusy != true)            {
                _backgroundworker.RunWorkerAsync();
            }
        }
        private void buttonCancel_Click(object sender, RoutedEventArgs e)        {
            if (_backgroundworker.WorkerSupportsCancellation == true)            {                _backgroundworker.CancelAsync();
            }
        }
        private void bw_DoWork(object sender, DoWorkEventArgs e)        {
            BackgroundWorker worker = sender as BackgroundWorker;
            for (int i = 1; (i <= 10); i++)            {                if ((worker.CancellationPending == true))                {                    e.Cancel = true;                    break;
                }                else                {                    System.Threading.Thread.Sleep(500);                    worker.ReportProgress((i * 10));                }
            }
        }
        private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {            if ((e.Cancelled == true))            {
                this.tbProgress.Text = "Canceled!";
            }
            else if (!(e.Error == null))            {
                this.tbProgress.Text = ("Error: " + e.Error.Message);
            }
            else            {                this.tbProgress.Text = "Done!";
            }
        }        private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {            this.tbProgress.Text = (e.ProgressPercentage.ToString() + "%");
        }
    }
}
```

---

Original Source: https://www.mindstick.com/blog/523/how-to-use-backgroundworker-class-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
