C#异步编程之:(三)使用TaskScheduler.UnobservedTaskException

当在C#中调用触发方法时不捕获AggregateException,.NET Framework会升级异常。默认情况下,这会在Task析构时重新抛出未处理的异常,导致程序终止。可以通过为System.Threading.Tasks.TaskScheduler.UnobservedTaskException添加事件处理程序来覆盖升级策略,以便在异常被提升时调用自定义代码。书中的示例无法工作,因为Task对象的析构函数永远不会被触发。经过修改,发现实际上只需要一个方法就能处理异常。

不多解释,抄书了:

If you don’t catch AggregateException when you call a trigger method, the .NET Framework will escalate the exceptions. By default, this means that the unhandled exceptions will be thrown again when your Task is finalized and cause your program to be terminated. Because you don’t know when the finalizer will be called, you won’t be able to predict when this will happen. But, you can override the escalation policy and supply your own code to call when an exception is escalated. You do this by adding an event handler to the static System.Threading.Tasks.TaskScheduler.UnobservedTaskException member.


书里面的实例根本没法用,永远不会触发Task对象的析构!所以我改写了下,而且书里面是2个方法都用了,其实任何一个方法都能处理异常!


using System;
using System.Collections;
using System.Threading.Tasks;

namespace Listing_22
{
    class Listing_22
    {
        static void Main(string[] args)
        {
            TaskScheduler.UnobservedTaskException +=
                (object sender, UnobservedTaskExceptionEventArgs eventArgs) =>
                {
                    // 阻止程序崩溃的方法有2种
                    
                    //第一种是:
                    {
                        eventArgs.SetObserved();
                        Console.WriteLine("Exception handled");
                    }

                    //第二种,返回true
                    if (false)
                    {
                        ((AggregateException)eventArgs.Exception).Handle(ex =>
                        {
                            Console.WriteLine("Exception handled");
                            return true;
                        });
                    }
                };

            RunTask();

            // 不断分配内存,强制让GC收集Task对象,从而触发UnobservedTaskException
            ArrayList arr = new ArrayList();
            while (true)
            {
                char[] array = new char[100000];
                arr.Add(array);
                GC.Collect();
            }
        }

        private static void RunTask()
        {
            new Task(() => { throw new NullReferenceException(); }).Start();
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值