博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c# Task编程一个task抛出异常后怎么取消其他线程
阅读量:5251 次
发布时间:2019-06-14

本文共 2472 字,大约阅读时间需要 8 分钟。

从MSDN的Forum上看到别人提供的解决方案,感觉还是比较靠谱,所以就保存下来。

            CancellationTokenSource cts = new CancellationTokenSource();

            Task t1 = Task.Factory.StartNew(() =>

            {

                if (!cts.IsCancellationRequested)

                {

                    try

                    {

                        //task body that may throw

                        Console.WriteLine("Task1");

                    }

                    catch

                    {

                        cts.Cancel();

                        throw new OperationCanceledException(cts.Token); //the task final state will be Cancelled

       //the exception can be ignored if needed

                    }

 

                    if (cts.IsCancellationRequested)

                    {

                        //depending on the scenario

                        //ignore any computed result, do not persist any data, revert all changes

                    }

                }

            }, cts.Token);

 

            Task t2 = Task.Factory.StartNew(() =>

            {

                if (!cts.IsCancellationRequested)

                {

                    try

                    {

                        //task body that may throw

                        Console.WriteLine("Task2");

                    }

                    catch

                    {

                        cts.Cancel();

                        throw new OperationCanceledException(cts.Token); //the task final state will be Cancelled

                    }

 

                    if (cts.IsCancellationRequested)

                    {

                        //depending on the scenario

                        //ignore any computed result, do not persist any data, revert all changes

                    }

                }

            }, cts.Token);

 

2.       In case that you would like to avoid try/catch in every body you could use a Task Continuation approach like below. However with this approach, the application will pay the price of new tasks being created. At the same time the Cancellation will be delayed.

            CancellationTokenSource cts = new CancellationTokenSource();

            Task t1 = Task.Factory.StartNew(() =>

            {

                if (!cts.IsCancellationRequested)

                {

                    //task body that may throw

                    Console.WriteLine("Task1");

                    if (cts.IsCancellationRequested)

                    {

                        //depending on the scenario

                        //ignore any computed result, do not persist any data, revert all changes

                    }

                }

            }, cts.Token).ContinueWith((task) =>

            {

                cts.Cancel();

                //observe the exception

                Exception ex = task.Exception;

            }, TaskContinuationOptions.OnlyOnFaulted|TaskContinuationOptions.ExecuteSynchronously );

 

            Task t2 = Task.Factory.StartNew(() =>

            {

                if (!cts.IsCancellationRequested)

                {

                    //task body that may throw

                    Console.WriteLine("Task2");

                    if (cts.IsCancellationRequested)

                    {

                        //depending on the scenario

                        //ignore any computed result, do not persist any data, revert all changes

                    }

                }

            }, cts.Token).ContinueWith((task) =>

            {

                cts.Cancel();

                //observe the exception

                Exception ex = task.Exception;

            }, TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously);

 

参考,转载:

https://social.msdn.microsoft.com/Forums/vstudio/en-US/2cbe1fa7-c7dd-4e88-8773-2e3bb1665e2e/how-to-cancel-other-task-when-there-is-an-exception-in-one-of-the-task?forum=parallelextensions

转载于:https://www.cnblogs.com/yy3b2007com/p/4145845.html

你可能感兴趣的文章
Mac版OBS设置详解
查看>>
优雅地书写回调——Promise
查看>>
android主流开源库
查看>>
AX 2009 Grid控件下多选行
查看>>
PHP的配置
查看>>
Struts框架----进度1
查看>>
Round B APAC Test 2017
查看>>
MySQL 字符编码问题详细解释
查看>>
perl 学习笔记
查看>>
31 Days of Windows Phone
查看>>
poj 1184(聪明的打字员)
查看>>
Ubuntu下面安装eclipse for c++
查看>>
C#压缩或解压(rar和zip文件)
查看>>
让IE浏览器支持CSS3圆角属性的方法
查看>>
巡风源码阅读与分析---nascan.py
查看>>
LiveBinding应用 dataBind 数据绑定
查看>>
Linux重定向: > 和 &> 区别
查看>>
nginx修改内核参数
查看>>
【欧拉函数模板题】最大公约数
查看>>
C 筛选法找素数
查看>>