How to use Asynchronous wait (await) at the entry point)

Source: Internet
Author: User
Tags call back

As we all know (except unknown), C #5 has added an asynchronous waiting function, which is very simple to use to Implement Asynchronous waiting. The rule is as follows:

Add the await keyword before calling the Asynchronous Method, and add an async keyword before calling the Asynchronous Method.

For example:

 
Async void dodelayasync () {await task. Delay (999999999 );}

When you enter the dodelayasync MethodCodeSynchronous execution: the application does not start asynchronous waiting until the await operator is encountered.ProgramIt will not die, which is quite good on the user interface. When the code is waiting for asynchronous operations to be completed at await, the user can still operate other elements on the interface.

Previously, to do this, we usually need to call back from another thread and use the delegate to call back. Now the await operator is n times simpler.

 

I received a spam email last night, which is indeed a spam email because I saw an unread email in the bin. I don't know who it is. I don't even dare to report my name. He raised this question.

Async static void main (string [] ARGs) {await task. Delay (8000); console. Read ();}

This dear friend was very powerful and even played an asynchronous wait at the entry point. I guess his inner world was so red that he was so happy that he could not describe it because he was about to discover the New World, await can also be asynchronous at the entry point. Later, I thought his inner world would surely be broken. I guess when he pressed F5, he found the following shocking scenes.

Obviously, the compiler does not want him to discover the New World, so it is a heavy blow to him.

 

Yes. If await is used for asynchronous wait within the method, the called method must be prefixed with async, but this method is still main. You cannot take it. What can I do now?

Some may say that it is easy to define a method and encapsulate an async method in the method, just like this.

 
Class program {static void main (string [] ARGs) {dodelayasync0 (); console. read ();} async static void dodelayasync0 () {await task. delay (3000); console. writeline ("Done, done ");}}

In this way, you can run it. This method is good.

Since method nesting can be solved, the result of delegation is equivalent.

Static void main (string [] ARGs) {Action Act = async () => {await task. Delay (5000); console. writeline ("wow, haha, haha. ") ;}; // Call the delegate Act (); console. Read ();}

In this way, the new world appears.

 

 

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.