BackgroundWorker is a class that is used to execute long running task on a thread different from your main application thread. You can run multithreaded applications using System.Threading namespace, but BackgroundWorker allows you to develop multithreaded applications with less fuss and bother with same syntax as you use for System.Threading namespace.
You can use BackgroundWorker by drag and drop BackgroundWorker control from Toolbox Components section or you can create the object for BackgroundWorker class. Here we discuss BackgroundWorker process by creating object for BackgroundWorker class.
To use BackgroundWorker, call RunWorkerAsync() method and pass required method to run in background. If you use BackgroundWorker in your application, the main application thread runs normally while the worker thread runs asynchronously.
BackgroundWorker class has two methods DoWork() and RunWorkerCompleted() methods to handle the background thread. In this example main thread handles the addition of two numbers and BackgroundWorker displays current time for every 5 seconds as shown below.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace BackgroundWorkerExp
{
public partialclassForm1 : Form
{
BackgroundWorker processBackGroundWorker = newBackgroundWorker();
public Form1()
{
InitializeComponent();
this.processBackGroundWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(this.processBackGroundWorker_DoWork);
this.processBackGroundWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.processBackGroundWorker_RunWorkerCompleted);
}
private void Form1_Load(object sender, EventArgs e)
{
processBackGroundWorker.RunWorkerAsync();
}
private void processBackGroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
System.Threading.Thread.Sleep(5000);
}
private void processBackGroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show(DateTime.Now.ToString(), "Current Time is: ");
processBackGroundWorker.RunWorkerAsync();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
int a, b;
a = Convert.ToInt32(txt1.Text);
b = Convert.ToInt32(txt2.Text);
MessageBox.Show((a + b).ToString());
}
catch (Exception ex)
{
throw ex;
}
}
}
}
As shown above we created the object for Background class and added the processBackGroundWorker_DoWork, processBackGroundWorker_RunWorkerCompleted methods for DoWork, RunWorkerCompleted events in Form constructor.
We are starting the BackgroundWorker thread by calling the RunWorkerAsync() method of BackgroundWorker class in form load. You are suspending the background thread by calling System.Threading.Thread.Sleep(5000) method in DoWork() event. After application startsfor every 5 seconds BackgroundWorker thread displays current time and if you click the button,main thread displays the addition of two numbers entered in text boxes.
The output displays as shown below.