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

本文共 2417 字,大约阅读时间需要 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

你可能感兴趣的文章
javascript高程3 学习笔记(三)
查看>>
lsof命令
查看>>
阿里云云计算ACP考试知识点(标红为重点)
查看>>
Unhandled event loop exception PermGen space
查看>>
从零开始来看一下Java泛型的设计
查看>>
嵌入式WiFi芯片价格战已经打响 MCU企业该醒悟了
查看>>
JavaScript格式化数字显示格式
查看>>
linux视频教程之vsftp_B
查看>>
Shell编程基础
查看>>
Android获取设备已安装的应用
查看>>
google code for xbmc addons2
查看>>
elasticsearch2.2之javaApi
查看>>
python3 UnicodeEncodeError: 'ascii' 错误
查看>>
SON Web Token设计单点登录系统
查看>>
避免活跃性危险(第十章)
查看>>
一个不成功人士的“成功之道”
查看>>
对大数据知识架构的梳理
查看>>
HTTPS实现原理
查看>>
MTD/MT/MDD/MD以及LIB/DLL之间的一些联系和问题
查看>>
SCVMM 2012 R2运维管理九之:添加非信任的Hyper-v主机和群集
查看>>