Asynchronous programming of MCAD Learning notes

Source: Internet
Author: User
Tags web services

(AsyncCallback delegate, IAsyncResult interface, BeginInvoke method, EndInvoke method using a small summary)

Let's look at the difference between synchronous asynchrony:

Synchronous method calls need to wait for the synchronization method to finish before the program continues execution returns the result

The asynchronous method returns immediately after being invoked so that the program performs other actions while the called method completes its task

. NET Framework base class libraries are available in several categories to provide synchronous and asynchronous method calls.

Because synchronous method calls can cause the program process to wait halfway through, the synchronization method often leads to delays in program execution

In comparison, it may be better to select asynchronous method calls under certain conditions

For example, there are times when a program needs to make requests to multiple Web services, as well as remoting channels (HTTP, TCP), and proxies, and it is best to use asynchronous methods

The. NET framework allows asynchronous invocation of any method that defines a delegate with the same signature as the method that needs to be invoked

The CLR will automatically define the BeginInvoke virtual method and EndInvoke virtual method and Invoke method for the delegate to add the appropriate signature.

A detailed description of these 3 methods of delegation can refer to this article

Http://www.cnblogs.com/aierong/archive/2005/05/25/162181.html

Let's take a look at these 2 methods and a delegate and an interface:

(1)

The BeginInvoke method is used to initiate an asynchronous call

It has the same parameters as the method you need to execute asynchronously, except there are two additional parameters that will AsyncCallback and AsyncState (available through the IAsyncResult interface

The AsyncState property is obtained as the last two arguments, if no can be null.

BeginInvoke immediately returns without waiting for the asynchronous call to complete.

BeginInvoke returns IAsyncResult, which can be used to monitor call progress.

The resulting object IAsyncResult is returned from the start operation and can be used to get a status about whether the asynchronous start operation has completed.

The resulting object is passed to the end operation, which returns the final return value of the call.

An optional callback can be provided in the start operation. If a callback is provided, the callback is invoked after the call is completed, and the code in the callback can call the end operation.

(2)

The EndInvoke method is used to retrieve the results of an asynchronous call.

You can call the EndInvoke method at any time after calling BeginInvoke, note: Always call EndInvoke after the asynchronous call completes.

If the asynchronous call is not completed, EndInvoke will block until the asynchronous call completes.

The EndInvoke parameters include the out and ref parameters of the method that need to be executed asynchronously and the IAsyncResult returned by BeginInvoke.

Note that the EndInvoke is always invoked after the asynchronous call completes

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.