可以防止其他AppDomain中的未捕获的异常关闭应用程序吗? [英] Can I prevent an uncaught exception in another AppDomain from shutting down the application?

查看:328
本文介绍了可以防止其他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 不会阻止异常冒泡,但我非常惊讶,没有其他方法可以在子AppDomain中捕获此类异常。

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".

插件主机或使用AppDomains的应用程序如何将可能有害的代码沙箱化,可以阻止未处理的异常吗?实际上可能吗?

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

注意:我已经有另一个解决方法,一个描述此处。坏的终结器是一个长寿命的对象,似乎只是在关闭时收集,所以从用户隐藏这个伪造错误就足够了。但是,我发现这个变通方法很脆弱,因为它会隐藏其他的,真实的错误,或者如果对象早先被收集的话,冒着冒险的风险。

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()在头部拍摄它,以防止终结器线程在进程退出时运行。使用.NET支持的进程之间的通信机制与之通信。一个套接字,名为pipe,Remoting或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.

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

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