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

查看:400
本文介绍了OperationCanceledException和TaskCanceledException之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是 OperationCanceledException TaskCanceledException 之间的区别?如果我使用.NET 4.5,并使用异步 / 等待关键字,其中一个我应该找赶上?

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?

推荐答案

<一个href=\"http://msdn.microsoft.com/en-us/library/system.operationcanceledexception.aspx\"><$c$c>OperationCanceledException简直就是<一个基类href=\"http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskcanceledexception.aspx\"><$c$c>TaskCanceledException - 所以,如果你赶上前,你还是会赶上后者

OperationCanceledException is simply the base class for TaskCanceledException - so if you catch the former, you'll still catch the latter.

这是并发集合一些操作只是扔 OperationCanceledException ,因为没有任何实际的任务的参与(至少尽可能的公共API被关注到)。见<一href=\"http://msdn.microsoft.com/en-us/library/dd381899.aspx\"><$c$c>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天全站免登陆