When you call the async method, it is initially synchronous. It does not even have the ability to be asynchronous until it reaches the wait.
For each pending expression, GetAwaiter() is called in the expected pending. The IsCompleted property is IsCompleted checked for awaiter. If the task is already completed, the method continues synchronously.
Otherwise, the OnCompleted method OnCompleted called in awaiter to add a continuation to it, which then returns when the task completes. The async method itself returns to the caller the first time it accesses a pending expression that is not yet complete.
The exact nature of the streaming depends on the student, but in async CTP for Task<T> , TaskAwaiter will use the current task scheduler to schedule a continuation. For WinForms / Silverlight / WPF, this means that if you run the asynchronous stream method in the user interface thread, it continues in the user interface thread. Otherwise (for example, if you are already using a stream stream or using it from a console application), the continuation will be performed in the stream stream. Of course, you can change the current task scheduler if you want.
Similar to other expectations, you do not need to plan a continuation using TaskScheduler.Current . For example, my coroutine continuations basically keep a continuation queue for execution and keep going until they go. My ComeFrom sequels are getting weirder :)
For more on how the async function works under the hood, read my Eduasync blog post
source share