Async & Await

Source: Internet
Author: User

A summary of new asynchronous programming features in 4.5

1. Async marked with async should use the await key to make a hang start. The await operator tells the compiler that the Async method cannot continue until it is finished, and that the control is transferred to the caller of the Async method

2. An async method typically contains one or more corresponding await operators, but does not cause compilation errors if no await expressions are present. But if you call an async method without using the AWAIT keyword to mark a hanging start, the program ignores the ASYNC keyword and executes it synchronously

3. If you specify task<tresult> as the return result, then this method must contain a statement that returns the TResult result specified by the return.

If a task is used as the return value, then the method should not have code that returns the result using the return statement, or the returned result does not participate in any operations (including assignment operations)

The 4.async and await keywords do not create additional threads, and the Async method does not request multithreaded operations. The operation that actually creates the thread is implemented by the Task.run ()

5. Inside an async method, when an asynchronous request (Httpclient.sendasync) is invoked, the other tasks of the main thread are processed, and the awaite waits for the completion of the asynchronous operation where the asynchronous operation is required to return the result Httpclient.sendasync tasks and tasks in the main thread are run in parallel (so you can take full advantage of the CPU's resources) there are two situations where a. An asynchronous task that SendAsync when calling Awaite has completed this time await Responsemsgtask is in the main thread, B at Awaite when the asynchronous task of SendAsync is not completed this time the main thread of the task will be blocked until the asynchronous task is completed and reactivate the main thread subsequent task this time await The Responsemsgtask is in the asynchronous thread

The code after 6.Await can also be placed in the task ContinueWith segment to run

Grammar sugar!!!

Transferred from: http://www.cnblogs.com/chengruhui/archive/2013/02/28/2937356.html

Async & Await

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.