C #5.0 asynchronous programming,

Source: Internet
Author: User

C #5.0 asynchronous programming,

In C #5.0, a powerful new feature-asynchronous programming is added. (. NET FrameWork4.5)

It appears with two new keywords:

  • · Async
  • · Await

1. The Async method has three return types:

  • Async void: it is an asynchronous operation that "triggers and forgets". The most common application scenario is the boundary between the async code and other code. For example, a void must be returned for UI event processing.

The caller cannot wait for any returned results, and cannot know when the operation ends or whether the operation is successful.

When you are sure that you do not need to know when the operation is completed or whether the operation is successful, you should use void.

Return statement only requires return; and is optional. (Do not write)

  • Async Task: allows the caller to wait for the result of the operation to end and pass exceptions during asynchronous code execution.

When we do not need to return any class values, an async Task method is better than the async void method because it allows callers to wait with await and it is easier to handle exceptions.

Return statement only requires return; and is optional. (Do not write)

  • Async Task <T>: for example, a Task <string> is usually used when a return value is required for an asynchronous operation.

Return must have a T-type expression, such as 5 + x, which must appear at the end of all paths of the method.

2. await try can be used, but it cannot be used in catch or finally. We can solve this problem through the following methods:

Bool failed = false;
try{   page = await webClient.DownloadStringTaskAsync("http://oreilly.com");}catch (WebException){   failed = true;}if (failed){   page = await webClient.DownloadStringTaskAsync("http://oreillymirror.com");}

3. Async lambda expressions

C # is not allowed to use the await keyword in the Query expression in most locations. This is because these locations are compiled into lambda expressions. Because of this, the lambda expression needs to be marked as an async keyword.

We can convert them as follows:

IEnumerable<int> transformed = from x in alexsIntswhere x != 9select x + 2;

You can use the extended method of the internal band of Linq. Then lambda expressions become clear and readable, And you can mark them as async to use await.

IEnumerable<Task<int>> tasks = alexsInts.Where(x => x != 9).Select(async x => await DoSomthingAsync(x) + await DoSomthingElseAsync(x));IEnumerable<int> transformed = await Task.WhenAll(tasks);

Reference: Chinese reprint http://www.cnblogs.com/tdws/p/5617242.html

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.