错误:该操作被取消

编程入门 行业动态 更新时间:2024-10-07 10:20:02
本文介绍了错误:该操作被取消的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用这个code段做一个取消标记一个异步查询:

I'm using this code snippet to do an async query with a cancellation token:

var _client = new HttpClient( /* some setthngs */ ); _client.GetAsync(someUrl, cancellationToken).ContinueWith(gettingTask => { cancellationToken.ThrowIfCancellationRequested(); SomeStuffToDO(); }, TaskScheduler.FromCurrentSynchronizationContext()); }, TaskScheduler.FromCurrentSynchronizationContext());

但是,当运行被取消, cancellationToken.ThrowIfCancellationRequested(); 抛出异常。我知道,这条线应该到这个东西。但是,在开发环境中,异常导致Visual Studio中休息。我怎样才能避免这种情况?

But, when operation get cancelled, cancellationToken.ThrowIfCancellationRequested(); throws an exception. I know that this line should to this stuff. But, in developing environment, the exception causes the visual studio to a break. How can I avoid this?

推荐答案

您需要将拉姆达内处理,就像这样:

You need to handle within the lambda, like this:

var _client = new HttpClient( /* some setthngs */ ); _client.GetAsync(someUrl, cancellationToken).ContinueWith(gettingTask => { try { cancellationToken.ThrowIfCancellationRequested(); SomeStuffToDO(); } catch (...) { ... } finaly { ... } }, TaskScheduler.FromCurrentSynchronizationContext()); }, TaskScheduler.FromCurrentSynchronizationContext());

但 _client.GetAsync(someUrl,的CancellationToken)也有可能抛出异常注销,所以你需要用的电话(或在其包含的方法等待)与一试-catch。

But _client.GetAsync(someUrl, cancellationToken) might also throw cancellation exception, so you need to wrap that call (or where its containing method is awaited) with a try-catch.

更多推荐

错误:该操作被取消

本文发布于:2023-11-03 04:56:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554290.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   操作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!