Essential C# Synchronization Context in Async/Await  Suggest an edit

Pseudocode for async/await keywords

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.

Source article.


Table Of Contents
22 Enum
25 GUID
107 Stream
108 Timers
113 Synchronization Context in Async/Await
  ↑ ↓ to navigate     ↵ to select     Esc to close