How APM is asynchronous programmatically in. NET C #

Source: Internet
Author: User
Tags apm

APM (asynchrocous programming model) mode:

To invoke a method asynchronously through a delegate class:

1. Define a delegate type:

delegate int Mydeletatetype (string s);

2. Instantiate a delegate instance, constructing a function to be called asynchronously Funca

var del = new Mydeletatetype (Funca);

int Funca (String a)
{
Console.WriteLine (a);
return 1;
}

Or

var del = new Mydeletatetype ((a) + = {Console.WriteLine (a); return 1;});

3. Invoke the method asynchronously by delegating:

Del. BeginInvoke ("Hello", null, NULL);

4. The above method is IAsyncResult BeginInvoke (string s,asynccallback callback,object @object)

String s corresponds to the Funa of the function that is called asynchronously, AsyncCallback callback is the delegate instance of the callback function, the object @object is a user-defined parameter that is stored in the IAsyncResult In the asyncstate of AR;

Where the delegate instance used for the callback function is

public delegate void AsyncCallback (IAsyncResult ar);

The BeginInvoke call returns IAsyncResult immediately, and the function Funca is called asynchronously. When the Funca is complete, the callback function is called, and the entry parameter in the callback function is the same copy of the IAsyncResult.

The

If you need to pass in a parameter to the callback function, you can use the object parameter in BeginInvoke, which is stored in IAsyncResult. In asyncstate, this parameter can be obtained in the callback function due to IAsyncResult. The asyncstate is read-only and the callback function does not modify the parameter. (If object is in a reference type, you can modify the content of the instance object)

Returns IAsyncResult after calling the BeginInvoke method,

You can use Iscompleted:bool to determine whether an asynchronous method is complete, when the Async method completes, then the flag bit is true, and then the callback function is started.

You can use Asyncwaithandle.waitone () to wait for the asynchronous call method to complete. WaitOne returns True after completion, there is an overloaded function WaitOne (Timespan timeOut), and so on, the specified wait for a certain time, if the time range of asynchronous method execution completes, immediately returns true, if the asynchronous method is not completed within the set time range, False is returned.

So:

You can use the method that polls the IsCompleted property, and the WaitOne () method to determine whether the Async method is complete.

Finally, how can the caller get the return value of the Async method after invoking the Async method. You can use the int EndInvoke (IAsyncResult ar) method in MyDelegate.

var del = new Mydeletatetype ((a) + = {Console.WriteLine (a); return 1;});
var asyncResult = Del. BeginInvoke ("Hello", null, NULL);
var result = Del. EndInvoke (AsyncResult);

The EndInvoke method waits for the asynchronous method to complete and return the result returned by the Async method.

The disadvantage of APM is that

1. Cannot cancel execution of asynchronous methods after invocation

2. The progress of asynchronous method execution is not known after the call

3. The Async method executes in another thread, and the caller's line Cheng Fei the same thread. Sometimes it can cause thread-safe problems.

To address these issues, Microsoft has introduced other asynchronous patterns.

How APM is asynchronous programmatically in. NET C #

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.