Use the backgroundworker component

Source: Internet
Author: User
Document directory
  • Start
  • Display Background operation progress
  • Cancel background operations
  • After the background operations are completed, feedback to the user
  • Return Value from background operation
  • End

The backgroundworker component is used to perform time-consuming asynchronous operations such as database transactions and file downloads.

Start

Add a backgroundworker instance to the application. If vs is used, you can drag it directly from the tool to the application:

Backgroundworker backgroundworker1 = new backgroundworker ();

To start operations in the background, you must call the runworkerasync () method of backgroundworker. When this method is called, backgroundworker starts background operations by triggering the dowork event, the dowork Event code is executed in another thread. The Code is as follows:

Code 1:

Backgroundworker backgroundworker1 = new backgroundworker ();

// Start the operation in another thread (btnstart is a button control)

// You can also use the runwokerasync () method to pass parameters,

Private void btnstart_click (Object sender, eventargs E)

{

Backgroundworker1.runwokerasync (2000/* the parameter is optional */);

}

// Execute the dowork event in another thread

Private void backgroundworkerincludowork (objectsender, doworkeventargs E)

{

// Allow long-time operations

Int input = (INT) E. argument;

Thread. Sleep (input );

}

Display Background operation progress

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.

Code 2:

Backgroundworker backgroundworker1 = new backgroundworker ();

Backgroundworker1.workerreportsprogress = true;

Private void btnstart_click (Object sender, eventargs E)

{

Backgroundworker1.runwokerasync ();

}

Private void backgroundworker=dowork (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 = E. progresspercentage;

}

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.

Code 3:

Backgroundworker backgroundworker1 = new backgroundworker ();

Backgroundworker1.workerreportsprogress = true;

Backgroundworker1.workersuppscanscancellation = true;

Private void btnstart_click (Object sender, eventargs E)

{

Backgroundworker1.runwokerasync ();

}

Private void btncancel_click (Object sender, eventargs E)

{

Backgroundworker1.cancelasync ();

}

Private void backgroundworker=dowork (Object sender, doworkeventargs E)

{

For (INT I = 1; I <11; I ++)

{

Thread. Sleep (2000 );

Backgroundworker1.reportprogress (I * 10 );

If (backgroundworker1.cancellationpending)

{

E. Cancel = true;

Return;

}

}

}

Private void backgroundworker1_progresschanged (Object sender, progresschangedeventargs E)

{

Progressbar1.value = E. progresspercentage;

}

 

After the background operations are completed, feedback to the user

After the background operation is complete, whether it is completed or canceled, the runworkercompleted event is triggered. This method can be used to feedback the result of the background operation to the user;

In addition, the cancelled attribute of the runworkercompletedeventargs instance is used to determine whether the cancel Operation terminates the background operation;

Code snippet:

Private void backgroundworkerappsrunworkercompleted (Object sender, runworkercompletedeventargs E)

{

If (E. cancelled)

{

MessageBox. Show ("Operation cancelled ");

}

Else

{

MessageBox. Show ("operationcompleted ");

}

}

Return Value from background operation

When a dowork event is executed, the result attribute of the doworkeventargs instance returns the value to the user. In the runworkercompleted event, the result attribute of the runworkercompletedeventargs instance receives the value;

Code snippet:

Private void backgroundworker=dowork (Object sender, doworkeventargs E)

{

Thread. Sleep (2000 );

// Set the return value here

E. Result = 10;

}

Private void backgroundworkerappsrunworkercompleted (Object sender, runworkercompletedeventargs E)

{

// Receive the passed value here

Int returnvalue = (INT) E. result;

}

End

 

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.