OperationCanceledException和TaskCanceledException之间的区别? [英] Difference between OperationCanceledException and TaskCanceledException?

查看:136
本文介绍了OperationCanceledException和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屋!

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