Introduction to asynchronous. net

Source: Internet
Author: User
Before learning Asynchronization, it is necessary to discuss "Why do we need to understand Asynchronization", that is, how to write Program What are the benefits. Now let's talk about synchronization. The so-called synchronization is executed in the program encoding order, and the rough point of the asynchronous statement is not executed in the encoding order, when do we need asynchronous programming? -------- if we encounter a task that is particularly time-consuming, because the following task is not related to the previous task, we can use Asynchronous execution (of course we can also use multiple threads). If you know when to use Asynchronization, let's start learning asynchronous programming.
1. asynchronous programming steps
1. Create a delegate with the same signature as the called Method

Assume that the called method is public void testmethod (string P)

Public Delegate void asyncdelegate (string P );

2. register the called Method to the delegated instance.

Asyncdelegate ad = new asyncdelegate (testmethod );

3. Call the Asynchronous Method starting with begininvoke

Iasyncresult AR = AD. begininvoke (string P, null, null)

4. Continue to perform operations that do not require asynchronous call results

// Perform other operations

5. Check the execution result of the asynchronous call method when the asynchronous call method result is required, and wait for the asynchronous call to complete synchronously.

While (AR. iscompleted = false)

{

Thread. Sleep (10 );

}

Ad. endbegininvoke ();

//Continue operation
II,Instance (attached to the backend)

3. In asynchronous callsUseIasyncresult(Indicating the status of asynchronous operations) andAsynccallback (Callback method executed when asynchronous call is complete)
1,Create a delegate with the same signature as the called Method

Assume that the called method isPublic void testmethod (string P)

Public Delegate void asyncdelegate (string P );

2,Register the called Method to the delegated instance

Asyncdelegate ad = new asyncdelegate (testmethod );

3,Define the callback method executed when the asynchronous call is completed

Public void callbackmethod (iasyncresult AR)

4,BegininvokeStart to call the Asynchronous Method

Iasyncresult AR = AD. begininvoke (string P, new asynccallback (callbackmethod), null)

5, Continue to do some operations that do not require asynchronous call results

//Perform other operations
4. instances (attached to the backend)
V. Summary of "Asynchronous Method signature"
Method signature is the order of parameter types in a method.Begininvoke The Asynchronous Method signature rules are as follows:

    • Including allIn Parameters.
    • Including allOut Parameters.
    • Including allIn/out Parameters.
    • Including allByrefParameters.
    • SetAsynccallback AndAsyncstate(You can useIasyncresult InterfaceAsyncstate Attribute) as the last two parameters.
    • ReturnIasyncresult.

Endinvoke The Asynchronous Method signature rules are as follows:

    • Including allIn/out Parameters.
    • Including allOut Parameters.
    • Including allByrefParameters.
    • SetIasyncresult As the last parameter.
    • Return the original return type from the original method signature.

Result object(Iasyncresult)Is returned from the start operation and can be used to obtain the status about whether the Asynchronous Start operation has been completed. The result object is passed to the end operation, which returns the final return value of the call. You can provide an optional callback when you start the operation. If a callback is provided, the callback is called after the call is completed.CodeYou can call to end the operation.

Source code:/Files/chinhr/code.rar


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.