我发现自己经常编写这样的代码:
try
{
cancellationTokenSource.Cancel();
await task.ConfigureAwait(false); // this is the task that was cancelled
}
catch(OperationCanceledException)
{
// Cancellation expected and requested
}
鉴于我请求取消,这是可以预料的,我真的希望忽略此异常。这似乎是一个常见情况。
有没有更简洁的方法来处理这个问题?我是否遗漏了有关取消的某些内容?看起来应该有一个 task.CancellationExpected()
方法或类似的东西。
await
它呢? - DavidG