C # overview of backgroundworker components (attributes, methods, and events)

Source: Internet
Author: User
C # overview of backgroundworker components (attributes, methods, and events)
Added the backgroundworker component in vs2005, which is very convenient to use in multi-threaded programming. However, at the beginning, it took a lot of detours because it was not clear about its usage mechanism, now I will share with you my experience in using it.
This attribute, method, and event are mainly used in the backgroundworker class:
Important attributes:
1. cancellationpending gets a value indicating whether the application has requested to cancel background operations. By judging the cancellationpending attribute in the dowork event, you can determine whether the background operation needs to be canceled (that is, the end thread );
2. isbusy gets a value indicating whether the backgroundworker is running an asynchronous operation. The isbusy attribute is used in the program to determine whether background operations are in use;
3. workerreportsprogress gets or sets a value that indicates whether the backgroundworker can report progress updates.
4. workersuppscanscancellation gets or sets a value that indicates whether backgroundworker supports asynchronous cancellation. Set workersuppscanscancellation to true so that the program can call the cancelasync method to submit a request to terminate the suspended background operation;
Important methods:
1. cancelasync requests to cancel pending background operations
2. runworkerasync submits a request to start an operation that runs asynchronously. After the request is sent, a dowork event is triggered and the background operation is started.
3. reportprogress raises the progresschanged event.
Important events:
1. occurs when dowork calls runworkerasync.
2. progresschanged when reportprogress is called
3. runworkercompleted occurs when the background operation is completed, canceled, or exception is thrown.
There are also three important parameters: runworkercompletedeventargs, doworkeventargs, and progresschangedeventargs.
The Calling mechanism and sequence of attributes, methods, and events of backgroundworker:

It can be seen that three important parameter transfer processes occurred throughout the life cycle:
Parameter Pass 1: This Parameter Pass is to pass the object in runworkerasync (object) to the doworkeventargs of the dowork event. argument, because only one parameter can be passed here, in actual application, encapsulate a class, and pass the entire instantiated class as the object of runworkerasync to doworkeventargs. argument;
Parameter transfer 2: This is to pass the program running progress to the progresschanged event. In actual use, it is often used to update the progress bar or log information for methods and events;
Parameter transfer 3: Before the dowork event ends, the result data generated by the background thread is assigned to doworkeventargs. result, while calling the runworkercompleted event runworkercompletedeventargs. Result attribute in the runworkercompleted event to obtain the result generated by the background thread.
In addition, we can see that the dowork event is run in the background thread, so the user interface content cannot be operated in this event. If you need to update the user interface, you can use the progresschanged event and runworkcompleted event.

After understanding the event calling sequence and parameter passing mechanism of bagkgroundworker, it is much easier to use this component for multithreaded programming.

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.