Use of C # async and await

Source: Internet
Author: User

This is a feature of. NET 4.5, so the requirement is minimal. NET version is 4.5.

See a lot of friends still use the thread to use asynchronous multithreaded operations, basically do not see the use of async, await asynchronous programming. Each their own, actually all can. As long as the correct use of the line, but still wrote this article recommended that you use Async, Await. The reason is that you can do asynchronous programming just as you would write synchronous methods. The code is very clear, just like writing normal code, without the relationship of how to do asynchronous programming, but also many novice programmers can also be programmed asynchronously. Here is an asynchronous example using thread multithreading implementation, and an asynchronous example using async and await, and then we'll take a quick and easy understanding of the technical notes for async and await.

Thread multithreaded Asynchronous Programming example
  1. Class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. Console. WriteLine("main thread test start ..." );
  6. thread th = new thread(thmethod);
  7. Th. Start();
  8. Thread. Sleep(+);
  9. Console. WriteLine("main thread test end:" );
  10. Console. ReadLine();
  11. }
  12. static void thmethod()
  13. {
  14. Console. WriteLine("Asynchronous execution Start");
  15. for (int i = 0; i < 5; I+ +)
  16. {
  17. Console. WriteLine("Asynchronous Execution" + i. ) ToString() + "..." );
  18. Thread. Sleep(+);
  19. }
  20. Console. WriteLine("Asynchronous execution Complete");
  21. }
  22. }

The above code runs the effect as

Asynchronous programming using Async and await
  1. Class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. Console. WriteLine("main thread test start ..." );
  6. Asyncmethod();
  7. Thread. Sleep(+);
  8. Console. WriteLine("main thread test end:" );
  9. Console. ReadLine();
  10. }
  11. static async void asyncmethod()
  12. {
  13. Console. WriteLine("Start async Code");
  14. var result = await MyMethod();
  15. Console. WriteLine("Asynchronous Code Execution Complete");
  16. }
  17. static async Task<int> MyMethod()
  18. {
  19. for (int i = 0; i < 5; I+ +)
  20. {
  21. Console. WriteLine("Asynchronous Execution" + i. ) ToString() + "..." );
  22. Await Task. Delay(+); //Analog time-consuming operation
  23. }
  24. return 0;
  25. }
  26. }

Operating effect:

It is obvious that we have written the asynchronous method as well as the synchronous method, and the code is clearer.

You can use the AWAIT keyword internally only if you have async. an async method can have a return type of Task, task<>, or void;

The await keyword is a method for returning a value that is a "waiting" type (awaitable)

Use of 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.