- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用一个支持取消的异步 api,我正在向该 api 传递一个 CancellationToken
实例。像往常一样,如果在传递的 token 上请求取消,我正在调用的 api 将抛出 OperationCanceledException
(这是 .NET 框架的标准协作取消模式)。
我希望能够 catch OperationCanceledException
当且仅当 这是由于取消提供的取消 token 而引发的异常。
以下代码说明了我要实现的目标:
try
{
await _service.DoSomethingAsync(cancellationToken: token);
}
catch (OperationCanceledException ex) when ( /* here I want a condition signifying that the OperationCanceledException is caused by the cancellation of the token object */)
{
// avoid logging the exception: this is raised by design (cooperative cancellation)
throw;
}
catch (Exception ex)
{
_logger.LogError(ex, "An error occurred: {0}", ex.Message);
throw;
}
对于上面代码的异常过滤器,我基本上有两个想法:
IsCancellationRequested
token
上的属性(property)对象:when (token.IsCancellationRequested)
CancellationToken
ex
上的属性(property)对象:when (ex.CancellationToken == token)
catch
完全阻止,并且只捕获一个
Exception
当且仅当它与
token
的取消无关目的。我知道这一点,但这不是我的问题的重点。
我在问题中编写代码只是为了清楚起见 ,因为我的问题的重点是如何正确关联
OperationCanceledException
与
CancellationToken
这导致了异常本身。
最佳答案
I want to be able to catch the OperationCanceledException if and only if it is the exception raised due to the cancellation of the provided cancellation token.
when (token.IsCancellationRequested)
是你想要的。
ex.CancellationToken
,因为您正在调用的方法可能正在观察链接的取消 token ,这与您提供的不同。
关于c# - 将 OperationCanceledException 关联到 CancellationToken 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67203796/
原因是如果它是CancellationToken.None,我需要添加一个超时(所以一个新的CancellationToken 来自CancellationTokenSource),例如 if (ca
我认为 RunAsync 方法采用 CancellationToken 作为参数,这很棒。 不幸的是,根据我的观察,我从未被取消。 当然,取消RunAsync 方法并调用OnCloseAsync 会有
我正在尝试使用取消标记从另一个线程中跳出循环: using System; using System.Threading; using System.Threading.Tasks; namespac
我对 AzureFunctions 还很陌生,所以请原谅我问一个简单的问题...... 我有以下代码,当队列中有一个项目时会触发该代码 [FunctionName("ProcessCompanies"
我有以下异步函数: private async Task ValidateFtpAsync() { return await Task.Run( ()
.NET 中的 TPL 新手。尝试了解 CancellationToken 以及它们如何收到取消正在执行的任务的信号。下面的代码只传输一个被取消的任务,因为相同的 token 被传递给两个任务。我的假
我有一个计时器,每 2 秒启动 2 个任务...我在一个简单的列表中跟踪这些任务(这样我就可以在停止应用程序时等待它们完成)。 任务有效地转到数据库,运行几个更新并完成。 任务本身的运行时间不会超过一
在没有搭建快速测试台的情况下;我想我会很快问 SO 看看是否有人知道这个答案。 此外,它可能还有一个额外的好处,即通知可能遇到类似情况的其他用户。 假设我有一个长期存在的 CancellationTo
我的 Xamarin.Forms 项目出现以下错误: at System.Threading.CancellationToken.ThrowOperationCanceledException (
我对如何为以下情况实现取消 token 感到有些困惑。 假设我有一个方法,它有一个取消 token ,没有像这样指定超时。 public static async Task DoSomeAsyncTh
.NET 4.5.1:看来,我无法使用 CancellationTokenSource 内置超时取消在任务中运行的阻塞方法。 class Program { static void Main(
我想使用锁或类似的同步来保护临界区。同时我想听一个CancellationToken。 现在我正在使用这样的互斥锁,但互斥锁的性能不佳。我可以使用任何其他同步类(包括新的 .Net 4.0)来代替互斥
这是我的情况: private CancellationTokenSource cancellationTokenSource; private CancellationToken c
我有一个用于各种任务的 token ,我需要更好地管理它们的取消,以便收到我可以使用的取消通知: token.Register(RegisterMethod); 我怎样才能删除这个“订阅”?有什么办法
我启动一个任务,然后启动其他任务等等。给定那棵树,如果任何任务失败,则整个操作的结果都是无用的。我正在考虑使用取消 token 。令我惊讶的是, token 没有“CancelThisToken()”
我今天早上一直在研究 Async CTP,并且有一个带有 button 和 label 的简单程序。单击按钮,它开始更新标签,停止按钮,它停止写入标签。但是,我不确定如何重置 Cancellation
我有一些异步代码,我想将其添加到 CancellationToken 中。但是,有很多实现不需要这样做,所以我想要一个默认参数 - 也许是 CancellationToken.None。然而, Tas
这段代码执行时 cancellationToken.ThrowIfCancellationRequested(); try catch block 不处理异常。 public Enumerab
最近我遇到了a Microsoft interface使用一个非常不寻常的 API: public interface IHostApplicationLifetime { public Ca
CancellationToken.CanBeCanceled 上的文档描述了它的作用,但没有提到它什么时候可能是假的。因此,我不完全清楚何时应该注意此属性。 在什么情况下它可以是假的,我应该什么时候
我是一名优秀的程序员,十分优秀!