How to use an asynchronous wait at an entry point (await)

Source: Internet
Author: User

As we all know (except for the unknown), C#5 has added an asynchronous wait function to be able to easily implement asynchronous waiting. The rule is this:

Precede the asynchronous method with the await keyword, and the method that calls the asynchronous method must precede a async keyword.

Like this:

Async void Dodelayasync ()  
{  
    await task.delay (999999999);  
}

The code executes synchronously while entering the Dodelayasync method, until the await operator is encountered, and the application does not die until the task is finished, which is pretty good in the user interface, when the code is waiting for the asynchronous operation to complete in await. Users can still manipulate other elements on the interface.

In the past, to do this, it is often necessary to recall from another thread, using a delegate to callback. Now using the await operator is simpler than n times.

I received a spam message last night, it was really junk mail, because I saw a unread letter in the trash bin. Do not know who, anyway, even the name is not afraid to report up. He raised such a question.

Async static void Main (string[] args)  
{  
    await task.delay (8000);  
    Console.read ();  
}

This man is very powerful, unexpectedly at the entry point also played the asynchronous wait, I guess he was the inner world must be colorful, open experience can not describe, because he is about to discover Newland, at the entry point can also asynchronous await. Then I thought that his inner world must have become China. I reckon that when he pressed the F5, he found the following fright.

Obviously, the compiler does not want him to find the new world, ruthlessly give him a heavy blow.

Yes, in the method inside with await asynchronous wait, that call method must be in front plus async, but this method is main, you take him no way. What can we do about it?

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.