1. Practice code full record:
usingSystem;usingSystem.Collections.Generic;usingSystem.Diagnostics;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;usingSystem.Threading.Tasks;namespaceawait_ Test {classProgram {Static voidMain (string[] args) {TESTC (); Console.read (); } //Async is contagious as a zombie virus, and async infects the surrounding code until the top level. In fact, we just have to let it go, so that all the code is infected with asynchronous features. //If we use task.wait or Task.result to try to block the spread of async, we often get bitter. //This is often the most easily made mistake by someone who has just come into contact with async async, which often leads to deadlocks. Wait for each other. Static Async voidTESTC () {////----notation 1. //Stopwatch sw = new Stopwatch (); //SW. Start (); //var D1 = Delay10 ();//execute immediately and wait for the result to be retrieved//var d2 = Delay20 ();//execute immediately and wait for the result to be retrieved//var d3 = delay30 ();//execute immediately and wait for the result to be retrieved ////Take results in a short time //await D1;//A single statement does not make sense, but similar to multiple statements in this scenario, the benefits of asynchronous concurrent execution come out. The total time taken is the longest time-consuming value. //await D2;//The first one executes, and the result is another. //await D3;//concurrent execution, "2" when a value is required, disable task.wait or task.result with await, prevent exception information, and prevent deadlocks//SW. Stop (); //Console.WriteLine ("End"); //Console.WriteLine ("Time Consuming:" + SW.) Elapsedmilliseconds); ////Conclusion: It takes about 25 seconds to do the same without affecting each other.////----notation 2. //Stopwatch sw = new Stopwatch (); //SW. Start (); //await Delay10 ();//linear execution, linear retrieval results//await Delay20 ();//linear execution, linear retrieval results//await delay30 ();//linear execution, linear retrieval results//SW. Stop (); //Console.WriteLine ("Time Consuming:" + SW.) Elapsedmilliseconds); ////Conclusion: time-consuming is 43 seconds, linear execution by line, the total time is more than three threads time-consuming sum. ////----notation 3. //Stopwatch sw = new Stopwatch (); //SW. Start (); //Task t1 = Delay10 ();//execute immediately,//Task t2= delay20 ();//execute immediately,//Task t3 = delay30 ();//execute immediately,//task[] tk = {T1, t2, T3}; //await Task.whenall (TK); //SW. Stop (); //Console.WriteLine ("Time Consuming:" + SW.) Elapsedmilliseconds); //////Conclusion: It takes about 25 seconds to do the same without affecting each other. //----notation 4. //Stopwatch sw = new Stopwatch (); //SW. Start (); //Task t1 = Delay10 ();//execute immediately,//Task t2 = Delay20 ();//execute immediately,//Task t3 = delay30 ();//execute immediately,//task[] tk = {T1, t2, T3}; //Task.waitall (TK); //Note: "1" needs to wait for all tasks to complete disabling task.waitall with await Task.whenall, preventing exception information and preventing deadlocks//http://www.cnblogs.com/bnbqian/p/4513192.html //SW. Stop (); //Console.WriteLine ("Time Consuming:" + SW.) Elapsedmilliseconds); //////Conclusion: It takes about 25 seconds to do the same without affecting each other.////Summary: Evolutionary process: thread+ thread synchronization semaphore, Task, async&await } Static AsyncTask Delay10 () {Task<string> t =Newtask<string> (() ={Thread.Sleep (25000);//small mistakes. Need to wait please use: Task.delay (), disable thread.sleep, prevent exception information, and prevent deadlocks return "25000"; }); T.start (); stringTR =awaitT; Console.WriteLine (TR); } Static AsyncTask Delay20 () {Task<string> t =Newtask<string> (() = {Thread.Sleep (10000);return "10000"; }); T.start (); stringTR =awaitT; Console.WriteLine (TR); } Static AsyncTask Delay30 () {Task<string> t =Newtask<string> (() = {Thread.Sleep (3600);return "3600"; }); T.start (); stringTR =awaitT; Console.WriteLine (TR); } }}
2. Practice NOTES:
1. Regardless of whether the method is synchronous or asynchronous, it can be identified with the Async keyword, because the async identifier only shows that the await keyword may be used in the method to make it asynchronous, and that the async method is explicitly partitioned, only if the await keyword is the asynchronous operation , and the rest are synchronous operations.
2. When you use Async to identify a method, only the display tells the compiler that the await keyword in the method may be used when execution to the await keyword starts in a pending state knowing that the asynchronous action execution is complete before resuming.
3. The async identification method does not affect whether the method is running synchronously or asynchronously, instead it can divide the methods into chunks, possibly running in async, so that the methods are asynchronous, and the boundary between the asynchronous and synchronous methods is to use the await keyword. That is, if the await keyword is not used in the method, the method is an entire block without the so-called partition, which runs in synchronization and is completed in synchronization.
"Notes" once. NET syntax await and async asynchronous programming experiments and notes.