What is the purpose of backgroundWorker? (can I get an example code for understanding?)

What is the purpose of backgroundWorker? (can I get an example code for understanding?)

Thanks in advance

+7
c #
source share
6 answers

In the background workflow, it is possible to unload long function calls to the background so that the interface does not freeze.

Suppose you have something that takes 5 seconds to calculate when a button is clicked. During this time, the interface will look β€œfrozen”: you will not be able to interact with it.

If a workflow stream was used instead, the button event set up the workflow and immediately returned. This will allow the interface to continue to accept new events, such as other button presses.

As for the code, here are 2 examples:

Here the interface will freeze

protected void OnClick( object sender, EventArgs e ) { CallLongRunningFunction(); // will take 5 seconds } 

It will not be here, since OnClick will immediately return, 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 that the call to the workflow will not be executed in the same thread as the interface, freeing it to continue normal operation.

+22
source share

BackgroundWorker is designed to allow you to perform heavy or lengthy operations in a separate thread using the user interface. If you run a lengthy process in a user interface thread, your user interface will likely freeze until the process completes.

However, BackgroundWorker takes a step forward by simplifying the threading process for you. If you have to create your own thread to perform a heavy or lengthy process, you will need to use delegates to access the user interface thread to update the progress bar, for example. With BackgroundWorker, you don’t need any delegates, because the BackgroundWorker ProgressChanged event is already running in the user interface thread, and you can add your interface update code there.

To start BackgroundWorker, you need to call RunWorkerAsync () :

 this.backgroundWorker.RunWorkerAsync(); 

To manually stop the call, execute CancelAsync () ...

 this.backgroundWorker.CancelAsync(); 

And then check the CancellationPending property from DoWork , as shown in the following 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 on how to use BackgroundWorker on my blog. You can check it here: Using the BackgroundWorker component in C #

+2
source share

It runs in the background using threads .

+1
source share

Do the work ... in the background? :)

From MSDN :

The BackgroundWorker class allows you to start operations on a separate, dedicated thread. Painstaking operations such as loading and a transaction database can cause the user interface (UI) to look as if it has stopped responding while they are running. When you need a responsive interface and are faced with long delays associated with such operations, the BackgroundWorker class provides a convenient solution.

There is also an extensive example on the same page . Here is another worthy textbook.

+1
source share

The purpose of the BackgroundWorker class is to provide you with an easy way to run operations in a separate thread.

It abstracts the process of creating and monitoring threads, providing you with an event-driven API to report the progress of the operation ( ProgressChanged ) and determine when your operation will be completed ( RunWorkerCompleted ) ..

Very useful when you need to perform time-consuming tasks that can make the user interface seem unresponsive.

+1
source share

The MSDN documentation has a sample C # code that illustrates what the background worker does and how to use it.

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

0
source share

All Articles