Consider a simple asynchronous method:
async Task Foo()
{
Bar();
await Baz();
Qux();
}
Simplifying, we can say that this code actually means the following:
Task Foo()
{
Bar();
Task t = Baz();
var context = SynchronizationContext.Current;
t.ContinueWith(task) =>
{
if (context == null)
Qux();
else
context.Post((obj) => Qux(), null);
}, TaskScheduler.Current);
return t;
}
It means that async
/await
keywords use current synchronization context if it exists. I.e. you can write library code that would work correctly in UI, Web, and Console applications.