是否可以防止子 AppDomain 中的未处理异常导致主进程崩溃?

Can Unhandled Exceptions in Child AppDomains be prevented from crashing the main process?(是否可以防止子 AppDomain 中的未处理异常导致主进程崩溃?)
本文介绍了是否可以防止子 AppDomain 中的未处理异常导致主进程崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个小型插件库,它使用应用程序域来隔离使用 .Net 框架 4.0 的插件.因此,每个插件中的代码都超出了我的控制范围.当其中一个插件引发未处理的异常时,我观察到结果好坏参半.它们如下.

I'm writing a small plugins library which uses app domains to isolate plugins using .Net framework 4.0. Hence the code that goes in each plugin is out of my control. When an unhandled exception is raised in one of the plugins, I have observed the results to be kind of a mixed bag. They are as follows.

当插件的主线程中抛出未处理的异常时,调用插件的执行方法的主可插入应用程序能够捕获并干净地处理它.那里没有问题.然而,

When the unhandled exception gets thrown in the plugin's main thread, the main pluggable app calling the plugin's execute method is able to catch and handle it cleanly. No problems there. However,

  1. 如果插件在插件的 Execute 方法中为基于 WinForms 的应用程序启动了一个消息循环,并且在 WinForm 应用程序中抛出了未处理的异常(即在表单中),则可插入应用程序只能捕获异常,如果它从 Visual Studio 调试器内部运行.否则(在 VS 外部调用时)主可插拔应用程序会与插件一起崩溃.

  1. If the plugin starts a message loop for a WinForms based app in the plugin's Execute method and an unhandled exception gets thrown in the WinForm application (i.e. in a form) then the pluggable app can only catch the exception if its running from inside visual studio debugger. Otherwise (when invoked outside VS) the main pluggable app crashes along with the plugins.

如果未处理的异常是在插件的 Execute 方法产生的单独线程中抛出的,那么可插拔应用程序就没有机会捕获异常并崩溃.

If the unhandled exception is thrown in a separate thread spawned by the plugin's Execute method, then the pluggable app has no chance of catching the exception and it crashes.

我在下面的链接中创建了一个简单的 VS 2010 项目来模拟这种行为.http://www.mediafire.com/file/1af3q7tzl68cx1p/PluginExceptionTest.zip

I have created a simple VS 2010 project to simulate this behaviour at the link below. http://www.mediafire.com/file/1af3q7tzl68cx1p/PluginExceptionTest.zip

其中的可插拔应用程序中的主要方法如下所示

In it the main method in the pluggable app looks like this

namespace PluginExceptionTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Press enter to load plugin");
            Console.ReadLine();

            Assembly entryAsm = Assembly.GetEntryAssembly();
            string assemblyFileName = Path.Combine(Path.GetDirectoryName(entryAsm.Location), "EvilPlugin.exe");


            AppDomainSetup domainSetup = new AppDomainSetup();
            AppDomain domain = AppDomain.CreateDomain("PluginDomain", null, domainSetup);
            PluginBase plugin = (PluginBase)domain.CreateInstanceFromAndUnwrap(assemblyFileName, "EvilPlugin.Plugin");

            Console.WriteLine("Plugin Loaded.");

            //SCENARIO 1: WinForms based plugin
            Console.WriteLine("Press Enter to execute winforms plugin. (Remember to click on the button in the form to raise exception)");
            Console.ReadLine();

            try
            {
                plugin.ExecuteWinApp();
            }
            catch (Exception)
            {
                //The exception is caught and this gets executed only when running in visual studio debugger. Else application exits. Why?
                Console.WriteLine("WinForms plugin exception caught. However same does not happen when run out of visual studio debugger. WHY?");
            }

            //SCENARIO 2: WinForms based plugin
            Console.WriteLine("Press Enter to execute threading plugin, wait for 3 seconds and the app will exit. How to prevent app from exiting due to this?");
            Console.ReadLine();
            try
            {
                plugin.ExecuteThread();
            }
            catch (Exception)
            {
                //This never gets executed as the exception is never caught. Application exits. Why?
                Console.WriteLine("WinForms plugin exception caught");
            }

            Console.ReadLine();
        }
    }
}

这是插件项目的代码.它继承自上述可插拔应用项目中的 PluginBase 类.

This is the code for the plugin project. It inherits from the PluginBase class in the pluggable app project above.

namespace EvilPlugin
{
    public class Plugin:PluginBase
    {
        public Plugin():base()
        {

        }

        public override void ExecuteWinApp()
        {            
            Application.Run(new Form1());            
        }

        public override void ExecuteThread()
        {
            Thread t = new Thread(new ThreadStart(RaiseEx));           
            t.Start();
        }

        private void RaiseEx()
        {
            Thread.Sleep(3000);
            throw new Exception("Another Evil Exception in a seperate thread");
        }
    }
}

最后这是插件中表单的代码

Finally this is the code from the Form in the plugin

namespace EvilPlugin
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnException_Click(object sender, EventArgs e)
        {
            throw new Exception("Evil Exception");
        }
    }
}

如何防止主进程由于上述两种情况(1 和 2)而退出?

How can i prevent the main process from exiting due to the two scenarios (1 and 2) mentioned above?

提前致谢.

推荐答案

用于处理 WinForms 异常.您可以在 PluginBase 类中为此类 ThreadExceptions 设置陷阱":

For handling WinForms exception. You could set "trap" for such ThreadExceptions in PluginBase class:

public abstract class PluginBase:MarshalByRefObject
{
    protected PluginBase()
    {
        System.Windows.Forms.Application.ThreadException +=
            (o, args) =>
            {
                throw new Exception("UntrappedThread Exception:" + args.Exception);
            };

    }

    public abstract void ExecuteWinApp();
    public abstract void ExecuteThread();
}

默认情况下,它会显示异常窗口".但是如果提供的话,会得到这个处理程序.

As by default, it will show you "The Exception Window". But will got to this handler if provided.

至于从其他线程捕获异常.没有办法.您能做的最好的事情就是收到有关抛出异常的通知.

As for catching exception from other thread. There is no way. The best you could do is have a notification about Exception being thrown.

    AppDomain domain = AppDomain.CreateDomain("PluginDomain", null, domainSetup);
    domain.UnhandledException +=
        (o, eventArgs) =>
            {
                Console.WriteLine("Exception was caught from other AppDomain: " + eventArgs.ExceptionObject);
                Console.WriteLine("CLR is terminating?: " + eventArgs.IsTerminating);
            };

这篇关于是否可以防止子 AppDomain 中的未处理异常导致主进程崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)