Async and await analysis and asyncawait Analysis

Source: Internet
Author: User

Async and await analysis and asyncawait Analysis

To understand the usage of async and await, first of all, we need to understand the knowledge of tasks, which is not described here, because this is not the focus of this article.

If you are familiar with tasks, you can use async and await to summarize the following three points:

 

The third point is a bit difficult, so let's talk about it with the code below:

        static void Main(string[] args)        {            Test();            Console.WriteLine("Test End!");            Console.ReadLine();        }        static async void Test()        {            await Test1();            Console.WriteLine("Test1 End!");        }        static Task Test1()        {            Thread.Sleep(1000);            Console.WriteLine("create task in test1");            return Task.Run(() =>            {                Thread.Sleep(3000);                Console.WriteLine("Test1");            });        }

 

Execution result:

 

The result is as follows:

The Main method calls the Test method with the async identifier. the Test method calls the Test1 method through await. When the execution is sequential to Test1, asynchronous operations are encountered; the Test method waits for the asynchronous operation in Test1 to be executed and then executes it. However, the Main method does not have to wait for the Test method to be executed and can continue to be executed.

 

 

In the above case, You can implement it without Using async or await. The Code is as follows:

        static void Main(string[] args)        {            Test();            Console.WriteLine("Test End!");            Console.ReadLine();        }        static void Test()        {            var test1=Test1();            Task.Run(() =>            {                test1.Wait();                Console.WriteLine("Test1 End!");            });        }        static Task Test1()        {            Thread.Sleep(1000);            Console.WriteLine("create task in test1");            return Task.Run(() =>            {                Thread.Sleep(3000);                Console.WriteLine("Test1");            });        }

 

 

 

Finally, this is my personal understanding and summary. You are welcome to discuss it or give me some advice.

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.