OnlyOnRanToCompletion 和 NotOnFaulted 之间的区别?
这两个值来自 TaskContinuationOptions 枚举。我有点不确定该使用哪一个。
另一对我感到困惑的是 NotOnRanToCompletion
和 OnlyOnFaulted
。
这个措辞对我来说有点令人困惑,每对中的每个值似乎都具有相同的功能。我在这里错过了什么吗?
These two values are from the TaskContinuationOptions enumeration. I'm a bit unsure of which one to use.
Another pair I'm confused between is NotOnRanToCompletion
and OnlyOnFaulted
.
The wording is a bit confusing to me and each value from each pair seem to function equally. Am I missing something here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的:如果某件事被取消,它既不是错误的,也不是运行完成的;因此它将由
NotOnRanToCompletion
处理,而不是由OnlyOnFaulted
处理。所以:
Yes: if something is canceled, it's neither faulted nor ran-to-completion; so it would be processed by
NotOnRanToCompletion
but not byOnlyOnFaulted
.So:
OnlyOnFaulted
表示如果前面的任务引发任务本身未处理的异常,则延续将运行,除非任务被取消。NotOnRanToCompletion
表示如果任务运行完成,延续将不会运行,也就是说,如果任务抛出异常,它将运行, 或者如果它被取消了。总而言之,如果您希望在任务取消或引发异常时继续运行,请使用
NotOnRanToCompletion
。如果您希望它仅在抛出异常时运行,但在取消时不运行,请使用OnlyOnFaulted
。OnlyOnFaulted
means that the continuation will run if the antecedent task throws an exception that is not handled by the task itself, unless the task was canceled.NotOnRanToCompletion
means that the continuation will not run if the task ran to completion, that is to say it will run if the task threw an exception, or if it was canceled.So to summarize, if you want your continuation to run if the task is canceled or threw an exception, use
NotOnRanToCompletion
. If you want it to run only if it threw an exception but not if it is canceled, useOnlyOnFaulted
.