About BackgroundWorker Process
When we have a function that takes a long time and it freezes the screen, the user gets nervous. They simply don't like to lose the control. It would give a friendly impression when they can still click on the form while the application is busy with getting the information.
For example, when you start implementing asynchronous methods, you will wonder which technique is best suitable for your application. Would you use a Timer class or a Thread class or a Background Worker? This post uses a BackgroundWorker class to show how easy it is, and where we need to pay attention when using it.
- There are three event handler methods are available to call the method asynchronously.
1.
DoWork Event Handler The DoWork method is like any other event handler.
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// Very time-consuming process or method.
}
2.
RunWorkerCompleted Method This event handler is used to display the status of the worker process after completion. So we can use for to display “Success” or “Failed” of the Process.
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (Convert.ToBoolean(e.Result) == true)
MessageBox.Show("Process List Loaded Successfully!");
else
MessageBox.Show("Process List Load Failed!");
}
3.
Progress Changed Method This is used to display the status of the process. For example we will have a Progress bar and each and every step increase the Progress status.
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e){ }Some of other important Keys are:1.e.Argument – Used to get the parameter reference received by RunWorkerAsync.
2.e.Result - Check to see what the BackgroundWorker processing did
3.backgroundWorker1.RunWorkerAsync(object)
Called to start a process on the worker thread.
4.DoWorkEventArgs e :
Contains e.Argument and e.Result, so it is used to access those properties.
How to Accessing the Win Forms Controls form the Background worker:- We can access through the anonymous methods of C#. The bellow example shows how to access the win forms combo box control through the background worker process.
- In this example retrieve the current process name list available of your system.

- So first add the namespace to Process, Thread class
using System.Diagnostics;
using System.Threading;
- To call background worker’s RunWorkerAsync() method
private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
- Next Background worker’s DoWorker Method
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// To call method directly
// bflg = LoadCombo();
// To call Using delegates
ToLoadCombo Tocall = new ToLoadCombo(LoadCombo);
e.Result = Tocall();
}
- Then Next Step we need to include LoadCombo()
//Public Method For Accessing Form Control (i.e CmbProcess)
public bool LoadCombo()
{
//Here is a simple way to update any UI controls from worker threads
// using anonymous methods and local variable capture.
try
{
new Thread(delegate() // anonymous methods
{
this.Invoke((ThreadStart)delegate() // anonymous methods Call
{
foreach (Process item in Process.GetProcesses())
{
cmbProcess.Items.Add(item.ProcessName.ToString());
}
cmbProcess.SelectedIndex = 0;
});
}).Start();
}
catch
{
return false;
}
return true;
}
- At Last we need to add RunWorkerCompleted Event Handler
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (Convert.ToBoolean(e.Result) == true)
MessageBox.Show("Process List Loaded Successfully!");
else
MessageBox.Show("Process List Load Failed!");
}
Using the above code we will develop faster and freezes the screen Application.