Examples of asynchronous programming for C # async and await

Source: Internet
Author: User


  
 
  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.



Null

Examples of asynchronous programming for C # async and await

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.