. Net4.5 usage of asynchronous programming (Async and Await) with new features

Source: Internet
Author: User

. Net4.5 usage of asynchronous programming (Async and Await) with new features
I. Introduction first, let's take a look at the features of various stages in the development of. net: 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 below I will briefly introduce asynchronous programming: asynchronous programming, in. NET Framework 4.5 and Windows use Asynchronous support during runtime. The compiler can execute the difficult work performed by developers, and the application retains a logical structure similar to synchronizing code. Therefore, you only need to do a small amount of work to get all the benefits of asynchronous programming. Asynchronous programming is based on the CPU idle time and multi-core features. The returned Task or Task <TResult> is 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 whose operand is TResult type, it is Task <TResult>. If your method does not return a statement or has a return statement without an operand, it is a Task. 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 commitment mentioned above, and the Asynchronous Method commitment will give the caller a result.) 3. Example practice is the best way to test the 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 ("End of Asynchronous Method");} private static async Task <int> SumAsync (int part) {if (part ++ = 10) >=100) {return 100 ;} httpClient client = new HttpClient (); Task <string> 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 (); Task <string> 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) implementation using asynchronous programming (2) the common synchronization method is used to implement 2. At the same time, recursion is implemented in this example. You don't need to consider this. The blogger just wants to verify whether recursion works in asynchronous mode, the experiment results are 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.

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.