C # Async and Await usage,

Source: Internet
Author: User

C # Async and Await usage,

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading; 6 using System. threading. tasks; 7 8 namespace CloudLearning. UI 9 {10 class Program11 {12 static void Main (string [] args) 13 {14 AsyncPrintHelloWorld (); 15 Console. readLine (); 16} 17 18 public async static void AsyncPrintHelloWorld () 19 {20 Console. writeLine ("Asynchronous Method call start"); 21 var result1 = TMothd1 (); 22 var result2 = TMothd2 (); 23 var result3 = TMothd3 (); 24 Console. writeLine ("Asynchronous Method completed"); 25 int r1 = await result1; 26 int r2 = await result2; 27 int r3 = await result3; 28 Console. writeLine ("{0}, {1}, {2}", r1, r2, r3); 29} 30 31 public async static Task <int> TMothd1 () 32 {33 return await Task. factory. startNew () => 34 {35 Thread. sleep (5*1000); 36 Console. writeLine ("TMothd1 completed"); 37 return 1; 38}); 39} 40 41 public async static Task <int> TMothd2 () 42 {43 return await Task. factory. startNew () => 44 {45 Thread. sleep (10*1000); 46 Console. writeLine ("TMothd2 completed"); 47 return 2; 48}); 49} 50 public async static Task <int> TMothd3 () 51 {52 return await Task. factory. startNew () => 53 {54 Thread. sleep (15*1000); 55 Console. writeLine ("TMothd3 completed"); 56 return 3; 57}); 58} 59 60} 61}View Code

 

Related Article

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.