Is .GetAwaiter().GetResult(); safe for general use?

后端 未结 1 725
北海茫月
北海茫月 2020-11-30 09:29

I read in a few places that .GetAwaiter().GetResult(); could cause deadlocks and that we should use async/await instead. But I see man

相关标签:
1条回答
  • 2020-11-30 10:05

    As I describe on my blog, GetAwaiter().GetResult() can deadlock when it's used in a one-thread-at-a-time context. This is most commonly seen when called on the UI thread or in an ASP.NET context (for pre-Core ASP.NET).

    Wait has the same problems. The appropriate fix is to use await, and make the calling code asynchronous.

    Note that the Main method in Console apps is an exception to this rule; it is perfectly appropriate to use there. Many code samples use it in this way.

    0 讨论(0)
提交回复
热议问题