Difference between .GetAwaiter() and ConfigureAwait() [closed]

末鹿安然 提交于 2019-12-23 23:10:04

问题


Can any one tell me difference between GetAwaiter() and ConfigureAwait(false).

Both of them are used in Async method to solve the deadlock situation and ConfigureAwait to complete task without using Synchrnoization context. I'm looking for scenarios where we can use GetAwaiter() and where we use ConfigureAwait(false).

I heard if it is library I'm building then I need to use ConfigureAwait(false) which generates Configurable Awaitable object of Await task. Can I use ConfigureAwait in Unittest case project or should use GetAwaiter() which get await task.


回答1:


Extracted from MSDN Docs

Task.GetAwaiter Gets an awaiter used to await this task. See more details here and here.

Task.ConfigureAwaiter Configures an awaiter used to await this task. See more details here and here




回答2:


Here is some guidance:

Async/Await - Best Practices in Asynchronous Programming

and another similar question:

Preventing a deadlock when calling an async method without using await

You can use Rx to mimic async operations in unit tests. I would advise against having actual async in unit tests; it would slow them down and discourage using those unit tests.



来源:https://stackoverflow.com/questions/33835063/difference-between-getawaiter-and-configureawait

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!