The summary of what is to be recorded today is:
When will the async code "Wait" there, when it won't "wait"?
The two days of coding encounter a matter, just want to let async wait where the results are directly carried out, such as:
1 Async Static voidCount ()2 {3Console.WriteLine ("Task Void starts");4 Console.WriteLine (Thread.CurrentThread.ManagedThreadId);5 intCount =0;6 while(Count <Ten)7 {8Console.WriteLine ("Print value:{0}", count);9count++;Ten awaitTask.delay ( -); One } AConsole.WriteLine ("Task Void End"); -}
Then write these two sentences:
1 var New Task (Count); 2 await taskvoid;
Haha, originally according to other asynchronous way to write the time such as await Httpclient.getasync (); is very normal, the logic of the program will stop in this sentence until the result is obtained before the following statement is made. But after running await Taskvoid, the following statement will run immediately, what's going on? After careful examination, it is found that the Count method has no return value and is void and therefore must be modified. If the Count method return value is a Task, await can be effective, such as changing to this:
1 Static voidMain (string[] args)2 {3 Entry ();4 console.readline ();5 6 }7 8 Async StaticTask Countasync ()9 {TenConsole.WriteLine ("Task Void starts"); One intCount =0; A while(Count <Ten) - { -Console.WriteLine ("Print value:{0}", count); thecount++; - awaitTask.delay ( -); - } -Console.WriteLine ("Task Void End"); + } - + Async Static voidEntry () A { at //to create a method with no parameters and no return value - varTaskvoid =Task.Factory.StartNew (countasync); - - awaitTaskvoid.result; - //methods to run after creating a taskvoid - varTaskcontinue = Taskvoid.continuewith (Async(t) = in { - toConsole.WriteLine ("This is the status of Taskcontinue,task void that will not run after the taskvoid is run {0}", t.status); + awaitTask.delay ( +); - }); the ; *}Post-transformation asynchronous
Personal Understanding:
Await as a syntactic sugar, the object that works is the task, and the Void method returns not the task nature does not work with the previous notation. The Task uses a "new thread" to execute the Void method, naturally "returns" immediately, but the return does not mean that the method is finished, and the method is running in accordance with its own logic, so this time I can only look at the void type Count return but unable to control it. After changing to the following, the modified Countasync method returns a task, meaning to tell the upper "I am a task", this time the await will be able to function. But the direct write await taskvoid still can not function, because at this time taskvoid no return value, namely immediately return, so must write "var taskvoid = Task.Factory.StartNew (countasync)" + " Await Taskvoid.result ", the StartNew method creates a new task internally and returns the return value of the task, which is Countasync, and Result indicates" I'm waiting for an asynchronous task to run Countasync this asynchronous task The result of the return ". Therefore, the await is in effect and the order of execution is correct.
I am not fine, to. Net within the basic nothing to dabble, if there is a veteran to see hope to make a suggestion, thank you.
. Net asynchronous Handy Note (i)