C # Asynchronous Learning One

Source: Internet
Author: User

Run the following code:

Static voidMain (string[] args) {Task T=tasktest ();}Private Static AsyncTask tasktest () {awaitDoWork (); Console.WriteLine ("tasktest");}Private Static AsyncTask DoWork () {Console.WriteLine ("Hello world!");  for(inti =0; I <3; i++)    {        awaitTask.delay ( +); Console.WriteLine ("working ... {0}", i); }}

The results of the operation are as follows:

Conclusion:

1. Async methods are marked async

2. The method marked async must use the await keyword, with the emphasis on await

  When the method runs with an await keyword, the compiler opens a separate thread that will run the code that follows it into the thread.

3. Code Execution analysis

Static voidMain (string[] args) {    //Program EntryTask t = tasktest ();//Enter Tasktest ()}Private Static AsyncTask tasktest () { await DoWork ();//encounter await, execute DoWork (), open a new thread after execution of Console.WriteLine ("Tasktest"); code    Console.WriteLine ("tasktest" );}Private Static AsyncTask DoWork () {Console.WriteLine ("Hello world!");//output "Hello wrold!" in console     for(inti =0; I <3; i++)    {         await task.delay (+);//encounter await, execute Task.delay (1000), open a new thread after executing the code        Console.WriteLine ("working ... {0} "    , i); }}

Based on the above code execution analysis, the console will eventually print only "Hello world!".

C # Asynchronous Learning One

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.