C# Async和Await的异步编程例子

Posted Long Long Journey

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# Async和Await的异步编程例子相关的知识,希望对你有一定的参考价值。


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. // Add a using directive and a reference for System.Net.Http;
  16. using System.Net.Http;
  17. namespace AsyncFirstExample
  18. {
  19. public partial class MainWindow : Window
  20. {
  21. // Mark the event handler with async so you can use await in it.
  22. private async void StartButton_Click(object sender, RoutedEventArgs e)
  23. {
  24. // Call and await separately.
  25. //Task<int> getLengthTask = AccessTheWebAsync();
  26. //// You can do independent work here.
  27. //int contentLength = await getLengthTask;
  28. int contentLength = await AccessTheWebAsync();
  29. resultsTextBox.Text +=
  30. String.Format("\r\nLength of the downloaded string: {0}.\r\n", contentLength);
  31. }
  32. // Three things to note in the signature:
  33. // - The method has an async modifier.
  34. // - The return type is Task or Task<T>. (See "Return Types" section.)
  35. // Here, it is Task<int> because the return statement returns an integer.
  36. // - The method name ends in "Async."
  37. async Task<int> AccessTheWebAsync()
  38. {
  39. // You need to add a reference to System.Net.Http to declare client.
  40. HttpClient client = new HttpClient();
  41. // GetStringAsync returns a Task<string>. That means that when you await the
  42. // task you‘ll get a string (urlContents).
  43. Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");
  44. // You can do work here that doesn‘t rely on the string from GetStringAsync.
  45. DoIndependentWork();
  46. // The await operator suspends AccessTheWebAsync.
  47. // - AccessTheWebAsync can‘t continue until getStringTask is complete.
  48. // - Meanwhile, control returns to the caller of AccessTheWebAsync.
  49. // - Control resumes here when getStringTask is complete.
  50. // - The await operator then retrieves the string result from getStringTask.
  51. string urlContents = await getStringTask;
  52. // The return statement specifies an integer result.
  53. // Any methods that are awaiting AccessTheWebAsync retrieve the length value.
  54. return urlContents.Length;
  55. }
  56. void DoIndependentWork()
  57. {
  58. resultsTextBox.Text += "Working . . . . . . .\r\n";
  59. }
  60. }
  61. }
  62. // Sample Output:
  63. // Working . . . . . . .
  64. // Length of the downloaded string: 41564.





以上是关于C# Async和Await的异步编程例子的主要内容,如果未能解决你的问题,请参考以下文章

抓住异步编程async/await语法糖的牛鼻子

C#的多线程——使用async和await来完成异步编程(Asynchronous Programming with async and await)

C#:异步编程中的 async 和 await

C# Async与Await的使用

C# async await 异步例子

C#异步方法async/await的三种返回类型