我可以prevent从关闭应用程序在另一个AppDomain中未捕获的异常? [英] Can I prevent an uncaught exception in another AppDomain from shutting down the application?

查看:144
本文介绍了我可以prevent从关闭应用程序在另一个AppDomain中未捕获的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不守规矩的库抛出的一个终结,这当然应用程序崩溃的异常麻烦。

I'm having trouble with a misbehaved library that throws an exception in a finalizer, which of course crashes the application.

要避免这种情况,我尝试加载在其自己的AppDomain库中,但异常仍气泡的表面和崩溃的应用程序。

To avoid this, I tried loading the library in its own AppDomain, but the exception still bubbles to the surface and crashes the application.

由于记录在MSDN上,注册到 AppDomain.UnhandledException 没有prevent异常向上冒泡,但我很惊讶,没有其他办法在一个子应用程序域抓这样的例外。

As documented on MSDN, registering to AppDomain.UnhandledException doesn't prevent the exception from bubbling up, but I'm quite surprised that there is no other way to catch such an exception in a "sub AppDomain".

如何插件的主机,或使用应用程序域沙箱可能有害code应用程序,请停止未处理的异常?它是实际上可能吗?

How do plugin hosts, or applications that use AppDomains to sandbox potentially harmful code, do to stop unhandled exceptions ? Is it in fact possible ?

注意:我已经有另外一个解决办法,一个描述<一href="http://stackoverflow.com/questions/1301292/why-do-appdomain-exceptions-invariably-terminate-the-application">here.坏的终结是一个长寿命的对象,这似乎在关机期间只抽取,所以它足以掩盖这种虚假的错误的用户。不过,我觉得这是解决办法脆,因为它要么隐藏其他的,真正的错误,或风险吹我的应用程序,如果该对象被早期收集。

Note: I already have another workaround, the one described here. The bad finalizer is on a long-lived object, which seems to be only collected during shutdown, so it is sufficient to hide this "bogus" error from the user. Still, I find this workaround brittle, since it will either hide other, real errors, or risk blowing up my application if the object is collected earlier.

推荐答案

一个AppDomain是好的,因为你能够摆脱程序的状态,但你仍然有该线程是死的问题。这发生在终结器线程是致命的,CLR将中止该过程无追索权。

An AppDomain is nice since you can ditch the program state but you still have the problem that the thread is dead. That this happens on the finalizer thread is fatal, the CLR will abort the process without recourse.

唯一的修复是运行该组件在自己的进程。您可以在头Process.Kill(),以prevent从在进程退出运行的投篮终结器线程。使用.NET支持谈论它的进程间通信机制之一。套接字,命名管道,远程处理或WCF。祝你好运吧。

The only 'fix' is to run this component in its own process. You can shoot it in the head with Process.Kill() to prevent the finalizer thread from running at process exit. Use one of the interprocess communication mechanisms that .NET supports to talk to it. A socket, named pipe, Remoting or WCF. Good luck with it.

这篇关于我可以prevent从关闭应用程序在另一个AppDomain中未捕获的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆