C # operations in the background: BackgroundWorker usage

Source: Internet
Author: User

In our programs, there are often some time-consuming operations. To ensure the user experience and avoid interface non-response, we usually use multi-threaded operations to complete time-consuming operations in the background, after the process is completed, or a prompt is displayed. During running, the progress bar on the interface will be refreshed from time to time. If necessary, the background thread must be controlled to interrupt the current operation.

In the past, similar applications were troublesome, requiring a lot of code to be written, and also prone to exceptions. In. net, a BackgroundWorker component is provided to solve this problem. The BackgroundWorker class allows you to run operations on individual dedicated threads. Time-consuming operations (such as downloads and database transactions) may cause the user interface (UI) to stop responding after a long time of running. You can use the BackgroundWorker class to solve the problem easily if you need a user interface that can respond and face long delays related to such operations.

This component is actually very simple. For example, we will make a small example of a progress bar similar to the following interface, perform time-consuming operations in the background thread, and refresh the progress bar on the interface.
The process is as follows:
1. Create a windows form application, such as BackgroundWorkerProgressBarDemo.
2. Drag a ProgressBar (progressBar1) and a BackgroundWorker (backgroundWorker1) to Form.
3. copy the following code. The code comments are detailed and can be modified as needed.

  +=             backgroundWorker1.WorkerReportsProgress =             backgroundWorker1.DoWork +=             backgroundWorker1.ProgressChanged +=  Form1_Shown(         backgroundWorker1_DoWork(             ( i = ; i <= ; i++                System.Threading.Thread.Sleep(         backgroundWorker1_ProgressChanged(            progressBar1.Value =

To prepare for background operations, add the DoWork event handler to call time-consuming operations in this event handler.

To start this operation, call RunWorkerAsync.

To receive a progress update notification, handle the ProgressChanged event.

To receive a notification when the operation is complete, handle the RunWorkerCompleted event.

Note:

Make sure that you do not operate any user interface objects in the DoWork event handler. You should use the ProgressChanged and RunWorkerCompleted events to communicate with the user interface.

BackgroundWorker events are not sent across AppDomain boundaries. Do not use the BackgroundWorker component to perform multi-threaded operations in multiple AppDomains.

If the backend operation requires parameters, provide the parameters when calling RunWorkerAsync. In the DoWork event handler, you can extract this parameter from the DoWorkEventArgs. Argument attribute.

Refer:

Http://msdn.microsoft.com/zh-cn/library/vstudio/System.ComponentModel.BackgroundWorker (v = vs.100). aspx

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.