C # async/await Usage Summary

Source: Internet
Author: User

Today the two key words to make a little dizzy, mainly still did not thoroughly understand the principle.

Confuses the concept of invoking an async method :

When you call an async method, the method returns a Task, but the code in it begins to execute. The method immediately executes a portion of the code at the time of invocation, directly at the bottom of the async API to produce a true asynchronous operation, which is then incrementally returned and eventually uses a task to represent the asynchronous task.

The Async method is also executed asynchronously when the await keyword is not used. After using the await keyword, it is simply a Task (awaitable) object that asynchronously waits for its execution to end, and then executes subsequent code in the same context.

If you use an await task. Configureawait (False), which indicates that the code after the line does not need to be executed in the same context.

In other words, for a call to Task Run ():

1. RunAsync (): Executes the Async method directly, followed by subsequent code execution.

2. Await RunAsync (): Executes the Async method and executes the subsequent code after the end (the code that precedes this line of code executes in the same thread).

3. Await RunAsync (). Configureawait (FALSE): Executes the Async method and executes subsequent code after the end (the thread being executed is not specified).

Use of Await

Also, because await is only for awaitable objects, it is not required to be used before an async method. You can use await at the appropriate time, for example:

var task = RunAsync ();//start asynchronous operation.

Dosth ();//The main thread performs other operations at the same time.

Await task;//at this time waiting for asynchronous execution to complete.

Dootherthing ();//Perform other operations.

Reference:

Below, list several articles related to async await

Dudu: Practical case: Implementing parallelism in existing code through ASYNC/AWAIT

Behind the implementation of the Async/await

Instructions for using async and await in MVC

Async & await Past life (Updated)

C # async/await Usage Summary

Related Article

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.