C#异步编程之:(三)使用TaskScheduler.UnobservedTaskException
2012-12-19 01:33
337 查看
不多解释,抄书了:
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个方法都用了,其实任何一个方法都能处理异常!
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(); } } }
相关文章推荐
- C#异步编程之:(三)使用TaskScheduler.UnobservedTaskException
- TaskScheduler.UnobservedTaskException
- 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(二)----使用方法
- 5天玩转C#并行和多线程编程 —— 第三天 认识和使用Task
- C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(二)
- C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志
- 解析C#多线程编程中异步多线程的实现及线程池的使用
- 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(一)----基础类库部分
- 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(一)----基础类库部分
- C# 线程知识--使用Task执行异步操作
- 在C#中使用异步Socket编程实现TCP网络服务
- 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(二)
- 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(二)----使用方法
- 5天玩转C#并行和多线程编程 —— 第三天 认识和使用Task
- 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(二)----使用方法
- 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(一)----基础类库部分
- C# 线程知识--使用Task执行异步操作
- C# 线程知识--使用Task执行异步操作
- [转载]在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(二)----使用方法
- (转自博客园-雲霏霏)5天玩转C#并行和多线程编程 —— 第三天 认识和使用Task