Allows through a clean api asynchronous functions i.e. delegation of the “timing of code execution” to the OS (Operating System) aka Promise Model of Concurrency.
how to use
1) Use async to designate asynchronous method.
2) Asynchronous methods must return one of the following: Task, void (send and forget) and Task<T> (if object/data needed to proceed).
3) Use await within the asynchronous method to “yield control” to the caller. i.e. caller execution continues. The asynchronous method execution will be “scheduled” to be completed by the OS.
4) For CPU-bound code use Task.Run (spawn of seperate thread), for I/O-bound code DO NOT USE Task.Run.
important
Add “Async” as the suffix to designate asynchronous methods.
To yeild control to caller, async methods must have await keyword.
Only use async void for event handlers.
Calling code awaiting tasks should be “non-blocking”.
Asynchronous code should minimize dependencies on state.
caution
Actual results of asynchronous programming using the above recommendations may differ depending on a particular situation. For example CPU-bound code execution may not be as “expensive” as the overhead of spawning a thread. Additionally, in a windows application spawned threads should never update the UI directly.