<legend id='H5wUs'><style id='H5wUs'><dir id='H5wUs'><q id='H5wUs'></q></dir></style></legend>
  • <tfoot id='H5wUs'></tfoot>
    • <bdo id='H5wUs'></bdo><ul id='H5wUs'></ul>

    <small id='H5wUs'></small><noframes id='H5wUs'>

    1. <i id='H5wUs'><tr id='H5wUs'><dt id='H5wUs'><q id='H5wUs'><span id='H5wUs'><b id='H5wUs'><form id='H5wUs'><ins id='H5wUs'></ins><ul id='H5wUs'></ul><sub id='H5wUs'></sub></form><legend id='H5wUs'></legend><bdo id='H5wUs'><pre id='H5wUs'><center id='H5wUs'></center></pre></bdo></b><th id='H5wUs'></th></span></q></dt></tr></i><div id='H5wUs'><tfoot id='H5wUs'></tfoot><dl id='H5wUs'><fieldset id='H5wUs'></fieldset></dl></div>

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

        Can Unhandled Exceptions in Child AppDomains be prevented from crashing the main process?(是否可以防止子 AppDomain 中的未处理异常导致主进程崩溃?)
      1. <legend id='7o5Vf'><style id='7o5Vf'><dir id='7o5Vf'><q id='7o5Vf'></q></dir></style></legend>
          • <small id='7o5Vf'></small><noframes id='7o5Vf'>

            • <bdo id='7o5Vf'></bdo><ul id='7o5Vf'></ul>

                <i id='7o5Vf'><tr id='7o5Vf'><dt id='7o5Vf'><q id='7o5Vf'><span id='7o5Vf'><b id='7o5Vf'><form id='7o5Vf'><ins id='7o5Vf'></ins><ul id='7o5Vf'></ul><sub id='7o5Vf'></sub></form><legend id='7o5Vf'></legend><bdo id='7o5Vf'><pre id='7o5Vf'><center id='7o5Vf'></center></pre></bdo></b><th id='7o5Vf'></th></span></q></dt></tr></i><div id='7o5Vf'><tfoot id='7o5Vf'></tfoot><dl id='7o5Vf'><fieldset id='7o5Vf'></fieldset></dl></div>
                  <tbody id='7o5Vf'></tbody>

                • <tfoot id='7o5Vf'></tfoot>

                  本文介绍了是否可以防止子 AppDomain 中的未处理异常导致主进程崩溃?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在编写一个小型插件库,它使用应用程序域来隔离使用 .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 中的未处理异常导致主进程崩溃?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Force JsonConvert.SerializeXmlNode to serialize node value as an Integer or a Boolean(强制 JsonConvert.SerializeXmlNode 将节点值序列化为整数或布尔值)
                  Using JSON to Serialize/Deserialize TimeSpan(使用 JSON 序列化/反序列化 TimeSpan)
                  Could not determine JSON object type for type quot;Classquot;(无法确定类型“Class的 JSON 对象类型.)
                  How to deserialize a JSONP response (preferably with JsonTextReader and not a string)?(如何反序列化 JSONP 响应(最好使用 JsonTextReader 而不是字符串)?)
                  how to de-serialize JSON data in which Timestamp it-self contains fields?(如何反序列化时间戳本身包含字段的JSON数据?)
                  JSON.Net custom contract serialization and Collections(JSON.Net 自定义合约序列化和集合)

                  • <bdo id='GusUX'></bdo><ul id='GusUX'></ul>

                      <legend id='GusUX'><style id='GusUX'><dir id='GusUX'><q id='GusUX'></q></dir></style></legend>
                      • <i id='GusUX'><tr id='GusUX'><dt id='GusUX'><q id='GusUX'><span id='GusUX'><b id='GusUX'><form id='GusUX'><ins id='GusUX'></ins><ul id='GusUX'></ul><sub id='GusUX'></sub></form><legend id='GusUX'></legend><bdo id='GusUX'><pre id='GusUX'><center id='GusUX'></center></pre></bdo></b><th id='GusUX'></th></span></q></dt></tr></i><div id='GusUX'><tfoot id='GusUX'></tfoot><dl id='GusUX'><fieldset id='GusUX'></fieldset></dl></div>
                        <tfoot id='GusUX'></tfoot>
                          <tbody id='GusUX'></tbody>
                          • <small id='GusUX'></small><noframes id='GusUX'>