C # Asynchronous Programming mode IAsyncResult

Source: Internet
Author: User

IAsyncResult Asynchronous design pattern implements asynchronous invocation of the original synchronization method through two methods named Beginoperationname and Endoperationname, such as the FileStream class provides BeginRead and EndRead methods To read bytes asynchronously from a file, which is an asynchronous version of the Read method

The Begin method contains any parameters in the synchronous method signature, plus two additional parameters: a AsyncCallback delegate and a user-defined state object. The delegate is used to invoke the callback method, which is used to pass state information to the callback method. The method returns an object that implements the IAsyncResult interface

The end method ends the asynchronous operation and returns the result, so it contains the ref and out parameters in the synchronous method signature, and the return value type is the same as the synchronization method. The method also includes a IAsyncResult parameter that gets the information about whether the asynchronous operation is complete, and of course the object instance returned by the corresponding Begin method must be passed in when it is used.

If you want to block an application after starting an asynchronous operation, you can call the End method directly, which prevents the application from continuing until the asynchronous operation completes. You can also use IAsyncResult's AsyncWaitHandle property to block threads by calling methods such as WaitOne in them. The difference between the two methods is small, but the former must wait while the latter can set the wait timeout

If you do not block the application, you can determine whether the operation is complete by using the iscompleted state of the IAsyncResult, or use the AsyncCallback delegate to end the asynchronous operation. The AsyncCallback delegate contains a IAsyncResult signature, and the callback method internally calls the End method to get the result of the operation execution

C # Asynchronous Programming mode IAsyncResult

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.