Executing threads with asynchronous delegates (delegate)

Source: Internet
Author: User

Because the thread that the asynchronous delegate produces is executed separately from the application's main thread, the main thread needs to wait for the result of the asynchronous delegate's execution if the main thread is to get the results of the asynchronous delegate threads.

BeginInvoke is a method of the delegate type whose return type is IAsyncResult, through which the execution information of the asynchronous delegate can be obtained.

1. Voting technology: One way to wait for the result of an asynchronous delegate is to vote, and the implementation of this technique is to check whether the delegate has completed the task through the Iscomplete property of the IAsyncResult interface. The Endinvote method gets the execution result. For example, the following:

The effect after execution:

2, access to wait handle technology: is the second method waiting for the result of the asynchronous delegate, in fact, using the AsyncWaitHandle property of the Iasyncresultasync interface to access the wait handle, through this property to obtain a WaitHandle object, The WaitHandle object has a method WaitOne ().

The code is as follows:

Execution effect:

Executing threads with asynchronous delegates (delegate)

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.