C#--await and Async for multithreaded asynchronous programming

Source: Internet
Author: User


Once, we may have used thread. When the main thread is running. A new thread is opened to run the new method.


Today to see someone sent me a piece of code to find a not know the await, but also feel very familiar with the look, feel is the thread that the thing, check the next, found a simple way to realize multi-threaded asynchronous programming.

(Ps:framework at 4.5 or more talent enough oh ~)




<summary>        //test method        ///</summary>//<remarks> Creator: Liu Huishuo; time: 2015-08-24 20:22:14 </remarks> public        async static  void Asyncprinthelloworld ()        {            Console.WriteLine ("Asynchronous Method call Start");            Console.WriteLine ("Async Method: Hello World");            . NET Framework 4.5 and above version number            await Task.delay (1000);//Open a new thread here to process, and make a test.

。 Console.WriteLine ("Exit Async Method"); }



Main call:


            Console.WriteLine ("=============== Async method call ===========");            Asyncprinthelloworld ();            Console.WriteLine ("************* is now running the main thread method **************");            Console.readkey ();

Method results:





Add Keywordasync to the method, at the same time, for the method where the new thread is to be opened. Add Awaitkeyword, then this will open another thread to handle, the main thread continues, the method here to run the results.



Very good fun, do not know which day to use.

。。。






C#--await and Async for multithreaded asynchronous programming

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.