The use of C # async and await

Source: Internet
Author: User
Tags readline

This is a. NET 4.5 feature, so the minimum is required. NET version is 4.5.

See a lot of friends or use thread to use asynchronous multithreaded operations, basically do not see the use of async, await asynchronous programming. Each of you, in fact, can. As long as the correct use of the line, but still wrote this article recommended that you use Async, Await. The reason for this is that you can do asynchronous programming like the Write Sync method. The code is very clear, just like writing normal code, do not have to deal with how to asynchronous programming, but also allow many novice programmers are also able to program asynchronously. Here is an asynchronous example using thread multithreading implementation, and an asynchronous example using async and await, and then we can briefly understand the async and await related technical instructions. thread Multithreading Asynchronous Programming example

Class Program {    static void Main (string[] args)     {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&N bsp;  Console.WriteLine ("Main thread test begins ...");         thread th = new Thread (Thmethod);         th. Start ();         Thread.Sleep (1000);         Console.WriteLine ("Main thread test end ...");         Console.ReadLine ();    }         static void Thmethod ()     {   & nbsp;    Console.WriteLine ("Asynchronous execution Start");         for (int i = 0; i < 5; i++)         {&NB sp;           Console.WriteLine ("Asynchronous Execution" + i.ToString () + "...");             Thread.Sleep (1000);        }         Console.WriteLine ("Asynchronous Execution Complete");    }}

The above code operation effect is as follows figure


using async and await for asynchronous programming

Class Program {    static void Main (string[] args)     {&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&N bsp;  Console.WriteLine ("Main thread test begins ...");         Asyncmethod ();         Thread.Sleep (1000);         Console.WriteLine ("Main thread test end ...");         Console.ReadLine ();    }       static async void Asyncmethod ()     {   & nbsp;    Console.WriteLine ("Start Asynchronous Code");         var result = await MyMethod ();         Console.WriteLine ("Execution of asynchronous code completed");    }       static async task<int> MyMethod ()     {         for (int i = 0; i < 5; i++)         {&NBSP;&NBSP;&NB sp;         Console.WriteLine ("Asynchronous Execution" + i.tostring () + "...");             await Task.delay (1000); Simulate time-consuming operations        }         return 0;    }}

Operation Effect:


It is obvious that we are doing the same as the write synchronization method, and the code is more clearly written.

You must have async to use the await keyword inside it. An asynchronous method can have a return type of Task, task<>, or void;

The await keyword is a method for returning a value that is a "can Wait" type (awaitable)

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.