Does ConfigureAwait (false) do anything

If in the library I have this

public async DoSomething()
{
   await Foo();
}

Then i call it like lib.DoSomething().ConfigureAwait(false).GetAwaiter().GetResult()

It will give me the same benefits as if the library originally had

public async DoSomething()
{
   await Foo().ConfigureAwait(false);
}
+4
source share
1 answer

No, it will not do you the same good. If you have:

public async Task DoSomething()
{
    await Foo();
}

And then do

lib.DoSomething().ConfigureAwait(false).GetAwaiter().GetResult()

From, for example, the user interface thread in a WPF application, you are at a dead end. Before await Foo()you were in the user interface thread (there was WPF-specific SynchronizationContext.Current), and after waiting you will try to return to the user interface thread. The user interface thread is blocked on GetResult(), which will lead to a deadlock. In fact, ConfigureAwait(false)it is useless here.

If, on the other hand, you have

public async Task DoSomething()
{
   await Foo().ConfigureAwait(false);
}

And do

lib.DoSomething().GetAwaiter().GetResult()

, ConfigureAwait(false) , " ", SynchronizationContext ( ).

+2

Source: https://habr.com/ru/post/1693626/


All Articles