如何使用CancellationToken取消任务?

编程入门 行业动态 更新时间:2024-10-27 00:23:44
本文介绍了如何使用CancellationToken取消任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以我有这段代码:

//CancelationToken CancellationTokenSource src = new CancellationTokenSource(); CancellationToken ct = src.Token; ct.Register(() => Console.WriteLine("Abbruch des Tasks")); //Task Task t = new Task(() => { System.Threading.Thread.Sleep(1000); if (ct.IsCancellationRequested) { try { //Throw ct.ThrowIfCancellationRequested(); } catch (OperationCanceledException) { Console.WriteLine( "ThrowIfCancellationRequested() liefert eben eine Exception"); } } }, ct); //Run Task and Cancel t.Start(); src.CancelAfter(350); t.Wait(); // Get Information Console.WriteLine("Canceled: {0} . Finished: {1} . Error: {2}", t.IsCanceled, t.IsCompleted, t.IsFaulted);

因此,在这种情况下,我取消了Task,但最终输出是:已取消:False。已完成:True。错误:False

So in this case I canceled my Task but my output in the end is: "Canceled: False . Finished: True . Error: False"

在我看来,它应该为 Canceled:True。Finished:False。 为什么会得到这个结果?因为我试图捕捉异常?

In my opinion it should be "Canceled:True . Finished:False". Why do I get this result? Because I try to catch the exception?

我尝试了没有try-catch块的尝试,但是由于OperationCanceledException,我的程序停止了。有人可以帮我吗?

I've tried it without the try - catch block, but then my program stops because of the OperationCanceledException. Can somebody help me?

推荐答案

您吞下了异常,因此当您实际处理异常时,该任务被标记为已完成并且不会向外传播。

You're swallowing the exception, thus the task is flagged as finished as you actually handle the exception and it doesn't propagate outwards.

相反,不要在委托内部捕获异常,而在外部捕获:

Instead, don't catch the exception inside the delegate, catch it outside:

void Main() { CancellationTokenSource src = new CancellationTokenSource(); CancellationToken ct = src.Token; ct.Register(() => Console.WriteLine("Abbruch des Tasks")); Task t = Task.Run(() => { System.Threading.Thread.Sleep(1000); ct.ThrowIfCancellationRequested(); }, ct); src.Cancel(); try { t.Wait(); } catch (AggregateException e) { // Don't actually use an empty catch clause, this is // for the sake of demonstration. } Console.WriteLine("Canceled: {0} . Finished: {1} . Error: {2}", t.IsCanceled, t.IsCompleted, t.IsFaulted); }

更多推荐

如何使用CancellationToken取消任务?

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

发布评论

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

>www.elefans.com

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