. 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:
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 );}}}
Example:
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.