Why does the asynchronous CTP (async CTP) work like that?

Source: Internet
Author: User

There are many reasons to be interested in asynchronous CTP. Asynchronous CTP makes asynchronous programming easier than ever. It's not as powerful as RX, but it's easier to learn. Asynchronous CTP introduces two new keywords, async and await. An async method (or lambda expression) must return Void,task or task<tresult>. This article is not about asynchronous CTP, because there are many such articles on the Internet. The purpose of this article is to focus the programmers on some of the common problems encountered with the async CTP. Inferred return type

When a value is returned from an async method, the method body returns the value directly, but the method itself is declared to return a task<tresult>. When declaring a method that returns a type must return a type B, it is a bit "disconnected".

// actual Syntax  Public Async task<int> GetValue () {  await taskex.delay (+);   return  - // The return type is "int", not "task<int>"}

Here's the question: Why can't I write this?

// Hypothetical Grammar  Public Async int GetValue () {  await taskex.delay (+);   return  - // The return type is "int"}

Thinking: How does this approach take care of callers? The Async method must return a value of the actual result type task<tresult>. As a result, the GetValue method will appear with smart hints that return task<tresult> (also in Object Browser, reflector, etc.).   At the beginning of the design, the inferred return type has been taken into account, but the design team has inferred that maintaining this "disconnection" in async methods is better than extending this "disconnection" over the code base. Now this "disconnection" still exists, but smaller than before. The consensus of the design team is that a consistent approach to signature is better. Thinking: What is the difference between async void  and async task? An async task method is just like any other asynchronous operation, except that there is no return value. An async void method acts as an advanced operation. The async task method may be combined into other asynchronous methods using the using await. The async void method may be used as an event handle. The Async void method also has other important properties: in the ASP. NET context, it notifies the Web server until it returns and the page finishes.   Inferred return types Remove the difference between async void  and async tasks: either all async methods are async void (block composable), or they are async tasks that prevent them from being handled from an event handle. At the same time, there is an alternative to ASP.   Asynchronous return   There is still a "disconnection" between the return type of the method declaration and the type returned by the method body. Another suggestion for the design team was to add a keyword to return to indicate the value returned by return, but this did not return anything as follows:
// Hypothetical Grammar  Public Async task<int> GetValue () {  await taskex.delay (+);   Async return  - // "Async return" means that the value is wrapped in a task }

Thinking: Converting large amounts of code from synchronous to asynchronous.

The async return keyword is also considered, but not convincing enough. This is especially true when you turn some of the synchronization code into asynchronous code. Forcing people to add asynchronous to each return statement is like "unnecessarily busy." By comparison, it is easier to be accustomed to "disconnection".

Infer "Async"

The Async keyword must be used on a method that uses the await keyword. However, if you use Async in a method that does not use await, you will receive a warning.

Question: Why can't async infer from the existence of an await?

// Hypothetical Grammar  Public The existence of task<int> GetValue  () {//  "await" implies that this is an "async" method.  await Taskex.delay (+);   return  - ;}

Thinking: Backwards compatibility and code readability

The word await keyword has too big to break the change. A multiple-word await (such as await for) on an async method or a selection between a keyword is only enabled within that method to enable the await keyword. Obviously, using the async tagging method makes it easier for humans and computers to analyze, so the design team decided to use async/await.

Infer "await"

Question: Since the display includes async makes sense (see above), why can't an await be inferred based on the existence of async?

// Hypothetical Grammar  Public Async task<int> GetValue () {  //  implies "await" because this is an "async" method.  Taskex.delay (+);   return  - ;}

Thinking: The parallel combination of asynchronous operations.

At first glance, inference to await inference seems to simplify the basic asynchronous operation. As long as all the wait can be completed by sequence (such as one operation waiting, then another, then another), this works well. However, when someone considers a parallel combination, it crashes.

The parallel combination in the asynchronous CTP uses the Taskex.whenany and Taskex.whenall methods. There is a simple example of this method that immediately starts two operations and waits for them to complete.

//actual Syntax Public Asynctask<int>GetValue () {//asynchronously retrieves a value of two parts//Note that at this point they are "not await" without waiting.task<int> part1 =GetValuePart1 (); Task<int> part2 =GetValuePart2 (); //wait for their values to arrive.   awaitTaskex.whenall (part1, part2); //to calculate our results  intValue1 =awaitPart1;//actually no wait  intvalue2 =awaitPart2;//actually no wait  returnValue1 +value2;}

To deal with parallel combinations, we have to be able to say that we will not await an expression.

Why does the

Asynchronous CTP (Async CTP) work like that?

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.