Search code examples
c#.net-coreasync-awaitconfigureawait

What is the difference between await and ConfigureAwait(false).GetAwaiter().GetResult();


Let's say I got an async method in C# .Net Core:

public Task<ResultClass> Action() { ... }

What is the difference between invoking: ResultClass res = await Action();

and invoking: ResultClass res = Action().ConfigureAwait(false).GetAwaiter().GetResult();


Solution

  • The await keyword will free the current thread during the work. So if you have a limited number of thread it's really useful.

    The "GetAwaiter().GetResult()" works synchronously so the current thread will be blocked during the work.

    The "ConfigureAwait(false)" is a configuration that have no sense if you are in synchronous code but can be usefull with the await

    await Action().ConfigureAwait(false);
    

    That you ensure the following will be called directly and avoid potential dead locks.