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

查看:23
本文介绍了我可以防止另一个 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 并不能阻止异常冒泡,但我很惊讶没有其他方法可以在子应用程序域".

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 支持的进程间通信机制之一与之对话.套接字、命名管道、远程处理或 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天全站免登陆