Async & await Asynchronous Programming Small example, you can understand

Source: Internet
Author: User

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 usingSystem.Threading;6 usingSystem.Threading.Tasks;7 8 namespaceConappasync9 {Ten     class Program One     { A  -         //The first step is to create a common time-consuming method -         Static stringGreeting (stringname) the         { -Thread.Sleep ( the); -             returnString.Format ("Hello, {0}", name); -         } +  -         //The second step is to wrap the above method with an asynchronous method +         Statictask<string> Greetingasync (stringname) A         { at             returntask.run<string> (() = -             { -                 returngreeting (name); -             }); -         } -  in         //The third step is to create a method that invokes the Async method above, plus the keyword async -  to         Private Async Static voidCallwithasync () +         { -             //some other tasks the  *             stringresult =awaitGreetingasync ("Wang Haibin"); $ Panax Notoginseng             //We can add multiple "await" in Same "async" method -             stringRESULT1 =awaitGreetingasync ("Ahmed"); the             stringRESULT2 =awaitGreetingasync ("every Body");  +Console.WriteLine (result+result1+result2); A         } the  +         Static voidMain (string[] args) -         { $             //Finally, call the method above at the main entrance, just as you would call the normal method $ Callwithasync (); -             intLength = -; -              for(inti =0; i < length; i++) the             { - Console.WriteLine (i);Wuyi             } the Console.readkey (); -         } Wu     } -}

Async & await Asynchronous Programming Small example, you can understand

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.