Reputation: 62554
what is the purpose of backgroundWorker ? (can i get some sample code to understand ?)
thank's in advance
Upvotes: 8
Views: 6341
Reputation: 199
The BackgroundWorker is designed to let you run heavy or long operations on a seperate thread to that of the UI. If you were to run a lengthy process on the UI thread, your UI will most likely freeze until the process completes.
However the BackgroundWorker goes a step further by simplifying the threading process for you. If you were to create your own thread to run the heavy or lengthy process you would need to use delegates to access the UI thread to update a progress bar for example. With the BackgroundWorker you don't need any delegates because the BackgroundWorker's ProgressChanged event already runs on the UI thread and you can add your UI updating code there.
To start the BackgroundWorker you need to call RunWorkerAsync():
this.backgroundWorker.RunWorkerAsync();
To manually stop it call CancelAsync()...
this.backgroundWorker.CancelAsync();
And then check the CancellationPending property from within the DoWork event as shown in the below example:
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
// Example heavy operation
for (int i = 0; i <= 999999; i++)
{
// Sleep for 10ms to simulate work
System.Threading.Thread.Sleep(10);
// Report the progress now
this.backgroundWorker.ReportProgress(i);
// Cancel process if it was flagged to be stopped.
if (this.backgroundWorker.CancellationPending)
{
e.Cancel = true;
return;
}
}
}
I wrote an article about how to use the BackgroundWorker on my blog. You can check it out here: Using the BackgroundWorker Component in C#
Upvotes: 3
Reputation: 828170
The purpose of the BackgroundWorker class is to provide you an easy way to run operations on a separate thread.
It abstracts the thread creation and monitoring process, giving you an event driven API to report the progress of the operation (ProgressChanged) and determine when your operation is finished (RunWorkerCompleted)..
Is very useful when you need to run time-consuming tasks that can cause the UI to seem unresponsive.
Upvotes: 1
Reputation: 2329
The background worker thread is there to help to offload long running function calls to the background so that the interface will not freeze.
Suppose you have something that takes 5 sec to compute when you click a button. During that time, the interface will appear 'frozen': you won't be able to interact with it.
If you used the background worker thread instead, the button event would setup the worker thread and return immediatly. That will allow the interface to keep accepting new events like other button clicks.
As far as code here are 2 examples:
Here the interface will freeze
protected void OnClick( object sender, EventArgs e )
{
CallLongRunningFunction(); // will take 5 seconds
}
Here, it won't as the OnClick will return immediately and the long running function will be executed in another thread.
protected void OnClick( object sender, EventArgs e )
{
BackgroundWorker bg = new BackgroundWorker();
bg.DoWork += new DoWorkEventHandler(bg_DoWork);
bg.RunWorkerAsync();
}
void bg_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
CallLongRunningFunction(); // will take 5 secs
}
The difference in behavior is because the call to the background worker thread will not execute in the same thread as the interface, freeing it to continue its normal work.
Upvotes: 22
Reputation: 48516
To do work... in the background? :)
From MSDN:
The BackgroundWorker class allows you to run an operation on a separate, dedicated thread. Time-consuming operations like downloads and database transactions can cause your user interface (UI) to seem as though it has stopped responding while they are running. When you want a responsive UI and you are faced with long delays associated with such operations, the BackgroundWorker class provides a convenient solution.
The same page also has an extensive example. Here's another decent tutorial.
Upvotes: 1
Reputation: 7894
The MSDN documentation has a C# code example illustrating what the backgroundworker does and how to use it.
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
Upvotes: 0