OperationCanceledException和TaskCanceledException之间的区别? [英] Difference between OperationCanceledException and TaskCanceledException?
问题描述
OperationCanceledException
和 TaskCanceledException
之间有什么区别?如果我正在使用.NET 4.5和使用 async
/ 等待
关键字,我应该寻找哪一个?
What is the difference between OperationCanceledException
and TaskCanceledException
? If I am using .NET 4.5 and using the async
/await
keywords, which one should I be looking to catch?
推荐答案
OperationCanceledException
只是 TaskCanceledException
- 所以如果你抓住前者,你仍然会抓住
OperationCanceledException
is simply the base class for TaskCanceledException
- so if you catch the former, you'll still catch the latter.
一些并发集合的操作只是 OperationCanceledException
,因为没有任何实际的涉及的任务(至少在公共API方面)。请参阅 BlockingCollection.TryTake
作为一个例子。
Some operations on concurrent collections throw just OperationCanceledException
, as there aren't any actual tasks involved (at least as far as the public API is concerned). See BlockingCollection.TryTake
for an example.
我将抓住 OperationCanceledException
,以防任务由于操作而被取消它本身只是抛出 OperationCanceledException
- 你可能仍然希望将其视为正在取消。
I would catch the OperationCanceledException
just in case the task is cancelled due to an operation which itself just threw OperationCanceledException
- you probably still want to treat that as "just cancellation".
这篇关于OperationCanceledException和TaskCanceledException之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!