摘要:Asynchronous programming in C# using async and await

  • 1750
  • 0

摘要與補充:Asynchronous programming in C# using async and await

The async keyword decorates the method and tells the compiler that this method may contain the await keyword.

When you're in doubt which method to use with 'await', here is the ultimate guideline: only methods that return Task or Task<T> can be awaited.


Rules and Limitations
The await keyword may not be used inside a catch or finally block (remember that await actually causes the method to return and later resume, and it's not possible to resume back into a catch / finally)
Constructors and property accessors cannot be marked as async and use await
An async method cannot have a ref or out parameters
The await keyword may not be used in the body of a lock statement


Hence the finally block in the code above will execute when the code (followed by await) in the try block completes (or if an exception is thrown), as it would in synchronous code.

參考資料:

[1]Asynchronous programming in C# using async and await
http://tech.pro/tutorial/1180/asynchronous-programming-in-c-using-async-and-await

 

補充資料:

[1]Asynchronous Programming in C# - Advanced Topics
http://tech.pro/tutorial/1229/asynchronous-programming-in-c-advanced-topics

[2]Asynchronous Controllers in ASP .NET MVC
http://tech.pro/tutorial/1252/asynchronous-controllers-in-asp-net-mvc