我什么时候应该使用ConfigureAwait(true) [英] when should I use ConfigureAwait(true)

查看:218
本文介绍了我什么时候应该使用ConfigureAwait(true)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人遇到使用ConfigureAwait(true)的情况吗?由于true是默认选项,因此我看不到您何时使用它.

Has anyone come across a scenario for using ConfigureAwait(true)? Since true is the default option I cannot see when would you ever use it.

推荐答案

我可以看到的一种可能性是,您是否正在库中编写代码,并且希望允许调用者确定在原始上下文中继续执行是否合适 1 (尽管我通常会争辩说永远不要在库代码中继续在原始上下文中进行操作)

One possibility I can see is if you're writing code in a library and you want to allow your callers to decide whether it's appropriate to continue on the original context1 (although I'd usually argue for never continuing on the original context from within library code)

您的调用者将传递bool参数或设置一些配置值,因此直到运行时您才知道正确的参数值是什么.

Your caller will either pass a bool parameter or set some configuration value, and so you will not know until runtime what the correct argument value is.

这是此类API的一般回答类型,具有无参数变体和带有单个参数的变体,其中无参数变体被记录为与具有良好参数的单参数变体相同-已知值 x "-如果直到运行时您才知道要传递的正确值是什么,那么您应该只使用正确的运行时值调用单个参数变量.

This is the general type of answer for APIs such as this that have a no-args variant and a variant with a single argument, where the no-args variant is documented as "the same as the single argument variant with well-known value x" - if you won't know until runtime what the correct value to pass is, then you should just call the single argument variant with the correct runtime value.

1 例如您的呼叫者还提供了一个代表.您的呼叫者将知道(并可以决定)该代表是否需要返回原始上下文.

1 E.g. your caller is also supplying a delegate. Your caller will know (and can decide) whether that delegate needs to be back on the original context or not.

这篇关于我什么时候应该使用ConfigureAwait(true)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆