126

My method returns Task. I want to wait until it finished. What should I use .Wait() or .GetAwaiter().GetResult()? What is the difference between them?

2 Answers2

149

Both are a synchronous wait for the result of the operation (and you should avoid those if possible).

The difference is mainly in handling exceptions. With Wait, the exception stack trace is unaltered and represents the actual stack at the time of the exception, so if you have a piece of code that runs on a thread-pool thread, you'd have a stack like

ThreadPoolThread.RunTask
YourCode.SomeWork

On the other hand, .GetAwaiter().GetResult() will rework the stack trace to take all the asynchronous context into account, ignoring that some parts of the code execute on the UI thread, and some on a ThreadPool thread, and some are simply asynchronous I/O. So your stack trace will reflect a synchronous-like step through your code:

TheSyncMethodThatWaitsForTheAsyncMethod
YourCode.SomeAsyncMethod
SomeAsync
YourCode.SomeWork

This tends to make exception stack traces a lot more useful, to say the least. You can see where YourCode.SomeWork was called in the context of your application, rather than "the physical way it was run".

An example of how this works is in the reference source (non-contractual, of course).

Luaan
  • 62,244
  • 7
  • 97
  • 116
  • 7
    [Task.GetAwaiter()](https://msdn.microsoft.com/en-us/library/system.threading.tasks.task.getawaiter%28v=vs.110%29.aspx) returns a [TaskAwaiter](https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.taskawaiter%28v=vs.110%29.aspx). However, the doc for [TaskAwaiter.GetResult()](https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.taskawaiter.getresult%28v=vs.110%29.aspx) advises: *"This API supports the product infrastructure and is not intended to be used directly from your code."* Can you comment? – DavidRR May 16 '16 at 19:49
  • 27
    @DavidRR The whole `TaskAwaiter` is an implementation detail. On the other hand, the awaitable/awaiter mechanism is documented, and uses duck-typing - `GetAwaiter` is to `await` as `GetEnumerator` is to `foreach` or `Dispose` is to `using`. All this is defined in the C# specification regardless of the particular awaiter being used - note that `Task.GetAwaiter` is "intended for compiler use rather than for use in application code." But the point is that the intended use is to do an `await`, not `Wait()` nor `GetAwaiter().GetResult()` - but `GetResult` gives you nicer stacks if you need it. – Luaan May 17 '16 at 08:03
  • 1
    @Luaan `using` and `Dispose()` does not use ducktyping. This only works when implementing `IDisposable`. Ducktyping is used for `foreach` and `GetEnumerator()` as for `await` and `GetAwaiter()` though. – codingdave Jan 25 '22 at 10:23
  • @codingdave Oh, you're right, there is a type-check while compiling the C# code. The native code doesn't actually do a virtual call, but that's not quite the same as a duck typing. – Luaan Jan 25 '22 at 12:05
-1

Try your best to avoid synchronously blocking on an asynchronous task.

In those rare exceptional cases, GetAwaiter().GetResult() will preserve the task exceptions,

If you use Wait it will throw AggregateException.

Refer to @stephen-cleary's blog post 'a-tour-of-task-part-6-results'

OzBob
  • 4,227
  • 1
  • 39
  • 48