C# async/await chaining with ConfigureAwait(false)

后端 未结 2 603
無奈伤痛
無奈伤痛 2020-12-19 02:01

Based on numerous books and blogs including this excellent one here, it is clear that when one writes a dll library exposing helper async methods i.e. the wrapper methods, i

相关标签:
2条回答
  • 2020-12-19 02:30

    Definitely not. ConfigureAwait just as it's name suggest configures the await. It only affects the await coupled with it.

    ConfigureAwait actually returns a different awaitable type, ConfiguredTaskAwaitable instead of Task which in turn returns a different awaiter type ConfiguredTaskAwaiter instead of TaskAwaiter

    If you want to disregard the SynchronizationContext for all your awaits you must use ConfigureAwait(false) for each of them.

    If you want to limit the use of ConfigureAwait(false) you can use my NoSynchronizationContextScope (see here) at the very top:

    async Task CallerA()
    {
        using (NoSynchronizationContextScope.Enter())
        {
            await Method1Async();
        }
    }
    
    0 讨论(0)
  • 2020-12-19 02:42

    When the task is awaited, it creates a corresponding TaskAwaiter to keep track of the task which also captures the current SynchronizationContext. After the task completes, the awaiter runs the code after the await ( called the continuation) on that captured context.

    You can prevent that by calling ConfigureAwait(false), which creates a different kind of awiatable (ConfiguredTaskAwaitable) and its corresponding awaiter (ConfiguredTaskAwaitable.ConfiguredTaskAwaiter) that does not run the continuation on the captured context.

    The point is that for each await, a different instance of an awaiter is created, it is not something that is shared between all the awaitables in the method or program. So it's best that you call ConfigureAwait(false) for each await statement.

    You can see the source code for the awaiters here.

    0 讨论(0)
提交回复
热议问题