. NET4.5 usage of asynchronous programming (Async and Await) with new features, asyncawait

Source: Internet
Author: User

. NET4.5 usage of asynchronous programming (Async and Await) with new features, asyncawait
I. Introduction

First, let's take a look at the features of various stages of. net development: each version of NET and C # has a "topic ". That is: C #1.0 hosting code → C #2.0 generic → C # 3.0LINQ → C #4.0 dynamic language → C #4.5 asynchronous programming

Asynchronous programming uses the CPU idle time and multi-core features. The tasks or tasks returned by the asynchronous programming are a commitment to await. After the Task is executed, a result is returned to the receiver. You may not understand this here. It doesn't matter. I will explain it below.

Ii. Instructions for use
  • The method signature contains an Async or async modifier.
  • According to the Conventions, the name of the Asynchronous Method ends with the suffix "Async.
  • The return type is one of the following:
    • If your method has a return statement of the TResult type with the operand, it is a Task.
    • If your method does not return a statement or has a return statement without an operand, it is a Task.
    • Sub in Visual Basic) if you're writing an async event handler. "> if you write an asynchronous event handler, It is Void (Sub in Visual Basic ).

    For more information, see "return types and Parameters" after this topic ".

  • A method usually contains at least one await expression, which marks a vertex and continues until the waiting Asynchronous Operation completes the method. At the same time, the method is suspended and the control returns to the caller of the method. (Here the so-called suspension is the above mentioned commitment, and the Asynchronous Method commitment will give the caller a result)

Iii. Example

Practice is the best way to test truth.

Using System; using System. diagnostics; using System. net. http; using System. threading. tasks; namespace asynchronous recursion {class Program {static void Main (string [] args) {Stopwatch stopwatch = new Stopwatch (); stopwatch. start (); ConsoleAsync1 (); stopwatch. stop (); Console. writeLine ("synchronous method:" + stopwatch. elapsedMilliseconds); stopwatch. reset (); stopwatch. start (); ConsoleAsync (); stopwatch. stop (); Console. writeLine ("Asynchronous Method:" + stopwatch. elapsedMilliseconds); Console. read ();} private static async void ConsoleAsync () {Console. writeLine ("Asynchronous Method start"); Console. writeLine ("Result:" + await SumAsync (10); Console. writeLine ("Asynchronous Method end");} private static async Taskint> SumAsync (int part) {if (part ++ = 10) >=100) {return 100 ;} httpClient client = new HttpClient (); Taskstring> getStringTask = client. getStringAsync ("http://msdn.microsoft.com"); Console. writeLine (DateTime. now. millisecond + "Asynchronous" + (await getStringTask ). length); return await SumAsync (part);} private static void leleasync1 () {Console. writeLine ("synchronization method start"); Console. writeLine ("Result:" + SumAsync1 (10); Console. writeLine ("synchronization method ended");} private static int SumAsync1 (int part) {if (part + = 10) >=100) {return 100 ;} httpClient client = new HttpClient (); Taskstring> getStringTask = client. getStringAsync ("http://msdn.microsoft.com"); Console. writeLine (DateTime. now. millisecond + "synchronization" + getStringTask. result. length); return SumAsync1 (part );}}}



1. There are two implementation methods in this example: (1) Implement through asynchronous programming (2) Implement through normal Synchronization

2. At the same time, recursion is implemented in this example. The blogger just wants to verify whether recursion works in asynchronous mode. The experimental result is valid.

3. The GetStringAsync () method in this code is used to obtain the content of the remote interface. The main purpose is to prolong the response time.

The program result is as follows:

Result description:

1. The synchronization method is conducted in an orderly manner according to the rules.

2. The Asynchronous Method is executed in 7 milliseconds. The execution process is asynchronous in the main thread.

Iv. slogan

Microsoft's official documents are worth learning. If you are interested, you can check them out. Here we will introduce a schematic diagram of the process. The example is shown in the document.

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.