Alice52 / c-tutorial

The repository is about c, including c, csharp, cpp.
MIT License
0 stars 0 forks source link

[async] .netcore thread #21

Open Alice52 opened 4 years ago

Alice52 commented 4 years ago

notice

  1. async method recommend response is Task, and should be forbidden used with void.

  2. await will sync the method.


more info

  1. you can only use await inside a method if that method is async and async methods need to return Task, Task<T> or void although void returning async methods are reserved for event handlers because the exceptions thrown within them are swallowed and you cannot await their completion or chain subsequent tasks.

  2. The reason of program shutdown when service throw an exception is the incorrect use of async & await. The docs about async & await as follow:

Async void methods are generally discouraged for code other than event handlers because callers cannot await those methods and must implement a different mechanism to report successful completion or error conditions.


reference

  1. Key word async
  2. Operator await
  3. Async & Await
  4. Return type