现在的位置: 首页 > 综合 > 正文

关于AppDomain 的 UnhandledException事件

2019年10月06日 ⁄ 综合 ⁄ 共 1687字 ⁄ 字号 评论关闭

这个事件实际上只是一个通知处理,并不是一个异常处理,在.net 2.0中,任何AppDomain的未处理异常都会导致进程的退出,你注册了这个事件只会在退出之前给你个通知,然后你就可以做一些日志或者记录。

你只能在托管线程上监听到AppDomain的这个事件。另外,这个事件必须在Default AppDomain中注册。

关于哪些未处理的异常会终止整个进程,哪些会被消化掉,CLR有一些默认的设置。一般来讲,线程池的线程以及终止线程(Finalizer)以及类似的“可重用”线程会被消化掉。CLR仅仅终止当前的工作单元,进行下一个。主线程上的托管执行代码抛出的未处理异常会终止整个进程。

这篇文章有一些详细的解释,并且描述了如何改变CLR的默认设置。

Managed Application Shutdown

 

另外,关于在.net 3.5下plugin的异常处理,请看如下描述:

The short answer is that up through .NetFX 3.5 there is no easy way to catch exceptions that go unhandled in child threads. If you use the unmanaged hosting API's to start up and manage the runtime you can use something called "escalation policy" to manage this but there are significant drawbacks to that approach (not the least of which is the amount of effort required).

 
The best you can generally do is detect which add-in (really which AppDomain) threw the unhandled exception and can do something interesting with that information (such as inform the user or disable the add-in). This is generally enough and is actually the strategy that office uses for its add-ins.

 If you're interested in this approach these two blog posts will show you how to do this in the context of our new NetFX 3.5 based add-in model but the techniques they describe are still valid on 2.0 and I believe 1.1:

 
http://blogs.msdn.com/clraddins/archive/2007/05/01/using-appdomain-isolation-to-detect-add-in-failures-jesse-kaplan.aspx

http://blogs.msdn.com/clraddins/archive/2007/05/03/more-on-logging-unhandledexeptions-from-managed-add-ins-jesse-kaplan.aspx

 
In general, if you are building your own extensible application with a plug-in/add-in model I encourage you to take a look at the blog. There is a lot of information there about using this new system as well as some good info for those who decide for some reason that they still need to build their own.

原始链接:http://forums.microsoft.com/MSDN/showpost.aspx?postid=1969048&siteid=1

 

抱歉!评论已关闭.