1.async await
private void Form2_load (object sender, EventArgs e) { Console.WriteLine ("Main:" + THREAD.CURRENTTHREAD.MANAGEDTHREADID); Dosth (); No await does not wait for dosth execution to complete directly eject Form2_load MessageBox.Show ("Form2_load"); } Async Task Dosth () { Do1 ();//No await does not wait for Do1 to finish (pop dosth executed after 3 pops do1 after 5s) equivalent to concurrency, add await will wait Do1 The subsequent execution of the line is equivalent to blocking await task.delay (in); Here can not use thread.sleep instead of the reason see the end of the red text (if not the thread Thread.Sleep will block the main thread) Console.WriteLine ("Dosth:" + THREAD.CURRENTTHREAD.MANAGEDTHREADID); MessageBox.Show ("Dosth executed after 3s"); } Async Task Do1 () { await task.delay (); Console.WriteLine ("Do1:" +thread.currentthread.managedthreadid); MessageBox.Show ("Do1 after 5s"); }
Thread ID Output 3 is the same reason (http://www.cnblogs.com/mushroom/p/4575417.html):
There is talk that Async does not have to use the thread, there is a need to say, from a single aspect are right, also is wrong. The above source code is simple analysis, the specific async internal will involve thread context switch, thread reuse, scheduling and so on. Want to deepen the classmate can study under Executioncontextswitcher, SECURITYCONTEXT.RESTORECURRENTWI, ExecutionContext these several east.
In fact, the details of the physical thread can not be too concerned about, know its "main thread a logic---" Asynchronous task thread B logic, the main thread C logic "the basic principle can be. In addition, Async will also be wired overhead, so you should be reasonable to use the business scenario.
From:http://www.cnblogs.com/xuejianxiyang/p/7089280.html
C # Async await