net4.5 the use of asynchronous programming (async and await) for new features

Source: Internet
Author: User

First, Introduction

Let's take a look at the various phases of the development of. NET: NET and every release of C # has a "theme". That is: c#1.0 managed code →c#2.0 generic →c#3.0linq→c#4.0 Dynamic language →c#4.5 Asynchronous programming

Let me briefly introduce asynchronous programming:Asynchronous programming that leverages asynchronous support in the. NET Framework 4.5 and the Windows runtime.The compiler can perform the hard work that the developer has done, and the application retains a logical structure similar to the synchronous code. So you can get all the benefits of asynchronous programming with just a small amount of work. (Https://msdn.microsoft.com/zh-cn/library/hh191443.aspx)

The so-called asynchronous programming is the use of CPU idle time and multi-core characteristics, the returned task or task<tresult> is a promise to await, when the task is completed, return a result to the receiver. Here you can see this may not quite understand, it does not matter, the following will be explained.

Ii. Description of Use
  • A method signature contains an async or async modifier.

  • By convention, the name of an async method ends with an "async" suffix.

  • The return type is one of the following types:

    • If your method has a return statement with an operand of type TResult, it is task<tresult>.

    • If your method does not return a statement or has a return statement with no operands, it is a Task.

    • If you are writing an asynchronous event handler, it is Void (a Sub in Visual Basic).

    For more information, see "Return Types and Parameters" later in this topic.

  • method typically contains at least one await expression that marks a point at which the pending asynchronous operation completes before the method can continue.

Iii. Examples

Practice is the best way to test truth.

usingSystem;usingSystem.Diagnostics;usingSystem.Net.Http;usingSystem.Threading.Tasks;namespaceasynchronous Recursion {classProgram {Static voidMain (string[] args) {Stopwatch Stopwatch=NewStopwatch (); Stopwatch.            Start ();            ConsoleAsync1 (); Stopwatch.            Stop (); Console.WriteLine ("synchronization method Time:"+stopwatch.            Elapsedmilliseconds); Stopwatch.            Reset (); Stopwatch.            Start ();            Consoleasync (); Stopwatch.            Stop (); Console.WriteLine ("Async Method Time:"+stopwatch.            Elapsedmilliseconds);        Console.read (); }        Private Static Async voidConsoleasync () {Console.WriteLine ("Asynchronous Method Start"); Console.WriteLine ("Result:"+awaitSumasync (Ten)); Console.WriteLine ("Asynchronous Method End"); }        Private Static Asynctask<int> Sumasync (intPart ) {            if(Part + =Ten) >= -)            {                return  -; } HttpClient Client=NewHttpClient (); Task<string> getstringtask = client. Getstringasync ("http://msdn.microsoft.com"); Console.WriteLine (DateTime.Now.Millisecond+"Asynchronous"+ (awaitgetstringtask).            Length); return awaitSumasync (part); }        Private Static voidConsoleAsync1 () {Console.WriteLine ("Synchronization method Start"); Console.WriteLine ("Result:"+ SUMASYNC1 (Ten)); Console.WriteLine ("end of synchronization method"); }        Private Static intSUMASYNC1 (intPart ) {            if(Part + =Ten) >= -)            {                return  -; } HttpClient Client=NewHttpClient (); Task<string> getstringtask = client. Getstringasync ("http://msdn.microsoft.com"); Console.WriteLine (DateTime.Now.Millisecond+"Sync"+getStringTask.Result.Length); returnSumAsync1 (part); }    }}

Examples are:

1. There are two implementations in this example: (1) using asynchronous Programming (2) using the common synchronous method to realize

2, at the same time, the implementation of this example recursion, this can not be considered, bloggers just want to verify that in the case of asynchrony, recursion is effective, experimental results are effective.

3. The Getstringasync () method in this code is used to obtain the contents of the remote interface, and the main purpose is to extend the response time.

The program results are as follows:

    

Result Description:

1, the synchronization method according to the rules, methodical.

2, the asynchronous method executes directly, spents 7 milliseconds. The execution process is asynchronous to the main thread.

Four, the tail language

Microsoft's official documentation is well worth learning and everyone is interested to see it. Here is a schematic diagram of the process, the corresponding example to the document to see, the link above has been given.

    

(source program interested can leave a message, I always provide)

net4.5 the use of asynchronous programming (async and await) for new features

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.