Multithreading (5) async&await

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.