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