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)