In Async/Await FAQ, Stephen Toub says:
An awaitable is any type that exposes a
GetAwaitermethod which returns a valid awaiter.
...
An awaiter is any type returned from an awaitable’sGetAwaitermethod and that conforms to a particular pattern.
So in order to be an awaiter, a type should:
- Implement the
INotifyCompletioninterface. - Provide a boolean property called
IsCompleted. - Provide a parameterless
GetResultmethod that returnsvoidorTResult.
(I'm ignoring ICriticalNotifyCompletion for now.)
I know the page I mentioned has a sample that shows how the compiler translates await operations but I'm stil having a hard time understanding.
When I await an awaitable,
- When is
IsCompletedchecked? Where should I set it? - When is
OnCompletedcalled? - Which thread calls
OnCompleted? - I saw examples of both directly invoking the continuation parameter of
OnCompletedand usingTask.Run(continuation)in different examples, which should I go for and why?