Asynchronous programming mode (II): Delegate-based asynchronous programming mode

Source: Internet
Author: User

In the last festival, how did the asynchronous programming mode displayed by the project be implemented?

Previous sectionCodeDefines a delegate:

Public Delegate long calculatefoldersizedelegate (string Foldername );

After the compiler, the following class templates are automatically generated when the preceding delegate statement is compiled:

 

Public sealed class calculatefoldersizedelegate: system. multicastdelegate
{
Public calculatefoldersizedelegate (object target, int methodptr ){.......}
Public void virtual invoke (string Foldername ){........}
Public Virtual iasyncresult begininvoke (
String Foldername, asynccallback callback, object asyncstate ){...........}

Publlic virtual void endinvoke (iasyncresult result ){.........}
}
 
 
 
When you use the normal synchronization mode to call the method directly through the delegate variable:
 
Calculatefoldersizedelegate d = calculatefoldersize;
 
Long size = D ("C: \ Windows ");
The compiler converts this call to a corresponding invoke method call. This is a synchronous call, which is executed in the same thread as the caller code.
 
To Implement Asynchronous calls, you must use the begininvoke and endinvoke methods instead of the invoke method.
 
 
 
1. About begininvoke
 
The begininvoke method generated by the compiler for the delegated data type is "like ":
 
Public iasyncresult begininvoke (<input and output variables>, asynccallback callback, object asyncstate)
 
The first part of the parameter is to define the parameter list in the method signature determined at the time of the delegate. The second parameter callback is the method automatically called back when the asynchronous call ends. The third parameter asyncstate is used
 
The callback method determined by the parameter provides additional information. For example, if the callback method has parameters, asyncstate can be used to fill in the parameter values.
 
These parameters will be detailed in the following Diary
 
Note that the begininvoke method returns the object of an iasyncresult interface.
 
This interface is defined as follows:
 
Public interface iasyncresult
 
{
 
Object asyncstate {Get ;}
Waithandle asyncwaithandle {Get ;}
 
Bool completedsynchronously {Get;]
 
Bool iscompleted {Get ;}
 
}
 
Note that the third parameter in the begininvoke method is encapsulated as the attribute asyncstate in the iasyncresult interface.
 
Another interesting field is iscompleted. The thread that starts the asynchronous call can use this field to check whether the asynchronous call is complete.
 
The completedsynchronously attribute of the bool type specifies whether the begininvoke call is synchronized.
 
The asyncwaithandle attribute of another member is a "waithandle (waiting handle)" object. When the asynchronous operation is complete, this handle is in the signaled state.
 
 
Ii. About endinvoke
 
The endinvoke method generated by the compiler for the delegated data type is like this:
 
Public <method return value type> endinvoke (<declared as ref or out parameter>, iasyncresult result)
 
As mentioned earlier, the begininvoke method is used to generate an iasyncresult interface type object and fill in its fields.
 
With the help of the iasyncresult object, the endinvoke method constantly queries whether the asynchronous call method has been executed.
 
The definition of the delegate type. The endinvoke method knows all the parameters of the method called asynchronously. Therefore, when the endinvoke method finds that the asynchronous call is complete,
 
It extracts the result of asynchronous call method execution as its return value. If the asynchronous call method is declared as a ref and out parameter, it is also responsible for filling it.
 
In this way, the caller thread that starts the asynchronous call can obtain the result of the asynchronous call method.
 
 
 
Because the endinvoke method has a continuous polling process, the main threadProgramIt is paused when the endinvoke method is called. Wait until the asynchronous call method is completed and retrieve
 
And then continue running.

 

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.