Backgroundworker Study Notes

Source: Internet
Author: User
1 Overview 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. 2. Use backgroundwork
  • To perform time-consuming operations in the background, create a backgroundworker to listen on events that report the operation progress and send signals when the operation is complete. You can create a backgroundworker by programming, or drag it from the toolbox, component tab, to the form. If you create a backgroundworker in the Windows Forms designer, it appears in the component bar and its properties are displayed in the "properties" window.
  • To prepare for background operations, add the dowork event processing program. This event handler calls time-consuming operations.
  • 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.
3. Create and use

Backgroundworker BW = new backgroundworker (); // background process
Bw. dowork + = new doworkeventhandler (bw_dowork); // bw_dowork some time-consuming operations will be performed here
Bw. runworkercompleted + = new runworkercompletedeventhandler (bw_runworkercompleted); // the event to be executed after the operation is completed
Bw. runworkerasync (); // This parameter can contain parameters. Optional.

Void bw_dowork (Object sender, doworkeventargs E)
{Time-consuming operation, E is an optional event parameter}

4. Display Background operation progress in order to display the execution progress of background operations, you must first set workerreportsprogress to true, then call the reportprogress () method of backgroundworker to pass the progress value of the operation. In addition, this method triggers the progresschanged event. In this event, parameters passed by the main thread are received through the progresschangedeventargs instance.
Backgroundworker backgroundworker1 = new backgroundworker (); worker = true; private void btnstart_click (Object sender, eventargs e) {worker ();} private void worker (Object sender, doworkeventargs E) {for (INT I = 1; I <11; I ++) {thread. sleep (2000); backgroundworker1.reportprogress (I * 10) ;}} private void backgroundworker1_progresschanged (Object sender, progresschangedeventargs e) {// progressbar1 is the progressbar control progressbar1.value =. progresspercentage ;}

 

5. Cancel background operations

To enable backgroundworker to cancel operations being performed in the background, set the value of workersuppscanscancellation to true. Then, call the cancelasync () method to set the cancellationpending attribute to true. You can use the cancellationpending attribute to determine whether to cancel the background asynchronous operation.

BackgroundWorker backgroundWorker1 = new BackgroundWorker(); backgroundWorker1.WorkerReportsProgress = true;backgroundWorker1.WorkerSupportsCancellation = true;

private void btnCancel_Click(object sender, EventArgs e) {       backgroundWorker1.CancelAsync(); }
if(backgroundWorker1.CancellationPending)  {     e.Cancel = true;      return;  }

Backgroundworker Study Notes

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.