• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#Task编程一个task抛出异常后怎么取消其他线程

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

从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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#postxmlusingHttpWebRequest/Response发布时间:2022-07-14
下一篇:
windowsAPI与C#的数据类型对应关系表发布时间:2022-07-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap