0

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();

CodeMonkey
  • 11,196
  • 30
  • 112
  • 203
  • 4
    `GetResult()` blocks the thread until the `Task` completes **which is a very bad thing**. You should never use `GetResult()`. – Dai Feb 24 '21 at 13:10
  • Do you mean it won't even return to the context of the caller? And is it the same as a regular sync call? – CodeMonkey Feb 24 '21 at 13:11

1 Answers1

3

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()" will do the same but 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.

Arcord
  • 1,724
  • 1
  • 11
  • 16
  • "The `GetAwaiter().GetResult()"` will do the same" - no, it does not "do the same but synchronously" thing as `await`: the thread calling `GetResult()` does not yield itself to the thread-pool: the thread is blocked instead. – Dai Feb 24 '21 at 13:23
  • I meant "the same work" and the second part of the sentence explains the differences. – Arcord Feb 24 '21 at 13:25
  • But it doesn't do "the same work" - it's _fundamentally different_. – Dai Feb 24 '21 at 13:27
  • 1
    I should maybe have specified "the same functional work" ? :p – Arcord Feb 24 '21 at 13:28
  • The only similarity between `await` and `GetResult()` is that *if nothing bad happens **and** the Task succeeds* - or if the Task is already completed - then the caller will have the `Task`'s `.Result` value - but that's it. – Dai Feb 24 '21 at 13:30
  • So if I understand correctly, when using GetResult(), the task will run on the same thread like a regular sync method, while with await, it will run on a different thread which will return to the thread pool at the end? – CodeMonkey Feb 25 '21 at 13:25
  • @Dai I think we a just not align about the term. By "Work" I mean what the method is supposed to do. By example a database call. In both case the database call (the work) will be done and the result will be the same. But the mechanism to acheive that work is obviously totally different under the hood. – Arcord Feb 28 '21 at 11:21
  • @YonatanNir The freed thread will come back into the thread pool if you use one. By example that's why it's important in ASP.NET to use "await" because you have a limited number of thread. – Arcord Feb 28 '21 at 11:22
  • @Arcord that's with await. And with GetResult, what will happen? Will it run a new thread? and if it does, what happens to it in the end? – CodeMonkey Feb 28 '21 at 13:26
  • With the GetResult the same thread still used. Meaning it will be blocked during the whole operation (network call, file system IO, ...). – Arcord Feb 28 '21 at 13:28