zoukankan      html  css  js  c++  java
  • c# Task编程一个task抛出异常后怎么取消其他线程

    从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

  • 相关阅读:
    Charles抓包工具的使用
    C++中引用(&)的用法和应用实例
    机器学习-斯坦福:学习笔记7-最优间隔分类器问题
    机器学习-斯坦福:学习笔记6-朴素贝叶斯
    机器学习-斯坦福:学习笔记5-生成学习算法
    机器学习-斯坦福:学习笔记4-牛顿方法
    机器学习-斯坦福:学习笔记3-欠拟合与过拟合概念
    机器学习-斯坦福:学习笔记2-监督学习应用与梯度下降
    机器学习-斯坦福:学习笔记1-机器学习的动机与应用
    相似性计算方法
  • 原文地址:https://www.cnblogs.com/yy3b2007com/p/4145845.html
Copyright © 2011-2022 走看看