The task of. NET 4.0 has made it very simple to use multiple threads, and can have return values, or support the cancellation of threads, which is already very powerful. But. NET 4.5 brings us async&await, making multithreading easier, more graceful, and more linear.
Here's an example of how the task and async&await are implemented separately, and finally the code execution sequence diagram is attached.
Using task implementations
The following code:
1 #regionUsing task implementations2 Static voidTestbytask ()3 {4Console.WriteLine ("main thread start,current thread ID:"+Thread.CurrentThread.ManagedThreadId);5 varTask = task.factory.startnew<string> (() =6 {7 returnGetnamebytask ();8 });9Console.WriteLine ("get another thread Result,result:"+task. Result);TenConsole.WriteLine ("Main thread completed!"); One } A - Static stringGetnamebytask () - { theConsole.WriteLine ("another thread start,current thread ID:"+Thread.CurrentThread.ManagedThreadId); - return "McGrady"; - } - #endregion
Output Result:
Using the Async&await implementation
If using async&await how to implement it, the following code:
1 #regionUsing the Async&await implementation2 Static Async voidtestbyasyncawait ()3 {4Console.WriteLine ("main thread start,current thread ID:"+Thread.CurrentThread.ManagedThreadId);5 varName =getnamebyasyncawait ();6 7Console.WriteLine (string. Format ("get another thread result,result:{0}",awaitname));8Console.WriteLine ("Main thread completed!");9 }Ten One Static Asynctask<string>getnamebyasyncawait () A { - return awaittask.factory.startnew<string> (() = - { theConsole.WriteLine ("another thread start,current thread ID:"+Thread.CurrentThread.ManagedThreadId); - return "McGrady"; - }); - } + #endregion
Output Result:
The output is the same as using a task.
The code execution process is as follows:
Full code:
1 namespaceConsoleApplication252 {3 class Program4 {5 Static voidMain (string[] args)6 {7 //1, using task implementation8 //Testbytask ();9 Ten //2, using async&await implementation One testbyasyncawait (); A - Console.readkey (); - } the - #regionUsing task implementations - Static voidTestbytask () - { +Console.WriteLine ("main thread start,current thread ID:"+Thread.CurrentThread.ManagedThreadId); - varTask = task.factory.startnew<string> (() = + { A returnGetnamebytask (); at }); -Console.WriteLine ("get another thread Result,result:"+task. Result); -Console.WriteLine ("Main thread completed!"); - } - - Static stringGetnamebytask () in { -Console.WriteLine ("another thread start,current thread ID:"+Thread.CurrentThread.ManagedThreadId); to return "McGrady"; + } - #endregion the * #regionUsing the Async&await implementation $ Static Async voidtestbyasyncawait ()Panax Notoginseng { -Console.WriteLine ("main thread start,current thread ID:"+Thread.CurrentThread.ManagedThreadId); the varName =getnamebyasyncawait (); + AConsole.WriteLine (string. Format ("get another thread result,result:{0}",awaitname)); theConsole.WriteLine ("Main thread completed!"); + } - $ Static Asynctask<string>getnamebyasyncawait () $ { - return awaittask.factory.startnew<string> (() = - { theConsole.WriteLine ("another thread start,current thread ID:"+Thread.CurrentThread.ManagedThreadId); - return "McGrady";Wuyi }); the } - #endregion Wu } -}
View Code
Multithreading (5) async&await