Jon
Jon

Reputation: 40032

Does ConfigureAwait(false) here achieve anything

If in a library I have this

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

I then call this like lib.DoSomething().ConfigureAwait(false).GetAwaiter().GetResult()

Would that give me the same benefit as if the library had originally had

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

Upvotes: 4

Views: 163

Answers (1)

Evk
Evk

Reputation: 101493

No it will not give you the same benefit. If you have:

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

And then do

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

From, for example, UI thread in WPF application - you will deadlock. Before await Foo() you were on UI thread (there was WPF-specific SynchronizationContext.Current) and after await you will try to return back to UI thread. UI thread is blocked on GetResult() so that will result in deadlock. Actually, ConfigureAwait(false) is useless here.

If on the other hand you have

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

And the do

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

No deadlock will happen, because ConfigureAwait(false) tells specifically to not "continue on captured context", so to not return back to SynchronizationContext (UI thread in this case).

Upvotes: 2

Related Questions