[英]How to set TaskContinuationOptions using async/await?
Can I have all the options like OnlyOnRanToCompletion, OnlyOnCanceled, NotOnFaulted, etc. using async/await? 我可以使用async / await等所有选项,如OnlyOnRanToCompletion,OnlyOnCanceled,NotOnFaulted等吗? I can't find examples on how to achieve the same results as using Tasks, for instance:
我找不到关于如何使用Tasks获得相同结果的示例,例如:
Task.Factory.StartNew(foo).ContinueWith(bar, TaskContinuationOptions.NotOnRanToCompletion);
I'm not sure if simple conditional or exception handling can manage all the continuation behaviors available in explicit Tasks. 我不确定简单的条件或异常处理是否可以管理显式任务中可用的所有延续行为。
Can I have all the options like OnlyOnRanToCompletion, OnlyOnCanceled, NotOnFaulted, etc. using async/await?
我可以使用async / await等所有选项,如OnlyOnRanToCompletion,OnlyOnCanceled,NotOnFaulted等吗?
You don't need to. 你不需要。
Instead of copious syntax using bit flags and lambda continuations, await
supports a very natural try
/ catch
syntax: 而不是使用位标志和lambda continuation的丰富语法,
await
支持非常自然的try
/ catch
语法:
try
{
await foo();
}
catch
{
bar();
throw;
}
I'm not sure if simple conditional or exception handling can manage all the continuation behaviors available in explicit Tasks.
我不确定简单的条件或异常处理是否可以管理显式任务中可用的所有延续行为。
They naturally handle None
, NotOnCanceled
, NotOnFaulted
, NotOnRanToCompletion
, OnlyOnCanceled
, OnlyOnFaulted
, and OnlyOnRanToCompletion
. 它们自然地处理
None
, NotOnCanceled
, NotOnFaulted
, NotOnRanToCompletion
, OnlyOnCanceled
, OnlyOnFaulted
和OnlyOnRanToCompletion
。 Most of the other flags only make sense for parallel tasks, not asynchronous tasks. 大多数其他标志仅对并行任务有意义,而不是异步任务。 Eg,
AttachedToParent
, HideScheduler
, and PreferFairness
don't make sense in the async
world; 例如,
AttachedToParent
, HideScheduler
和PreferFairness
在async
世界中没有意义; DenyChildAttach
, LazyCancellation
, and ExecuteSynchronously
should always be specified in the async
world; 应始终在
async
世界中指定DenyChildAttach
, LazyCancellation
和ExecuteSynchronously
; and LongRunning
never should be. 和
LongRunning
永远不应该。
I don't think so. 我不这么认为。
Async/await was not made to replace TPL all together, but to supplement it by making simple operations cleaner. Async / await并没有一起取代TPL,而是通过简化操作来更好地补充它。
If you still need extra configuration, you'll have to stick to tasks, or you could try implementing a custom awaiter with this behavior. 如果您仍需要额外配置,则必须坚持使用任务,或者您可以尝试使用此行为实现自定义等待。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.