【问题标题】:Windows Forms Unhandled Exception CrashWindows 窗体未处理的异常崩溃
【发布时间】:2021-02-05 11:06:05
【问题描述】:

我的代码可以让我的 winforms 应用程序处理未处理的异常。但是我的应用程序仍然崩溃。

在这个阶段,我确实不明白为什么会出现这种行为。非常感谢您的帮助。

这是我的代码:

 [STAThread]
    private static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        bool result;
        var mutex = new System.Threading.Mutex(true, "MyApplication", out result);
        if (!result)
        {
            MessageBox.Show("Another instance is already running.");
            return;
        }
        Application.ThreadException += ApplicationThreadException;
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
        AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException;
        Memory.frmMain = new MainForm();
        Application.Run(new MyApplicationContext());
        GC.KeepAlive(mutex);
    }
    public static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        MessageBox.Show(((Exception)e.ExceptionObject).Message);
        ((Exception)e.ExceptionObject).AddLog();
        Memory.processtranslations.IsProcessing.Enabled = true;
    }
    public static void ApplicationThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
    {
        MessageBox.Show(e.Exception.Message);
        e.Exception.AddLog();
        Memory.processtranslations.IsProcessing.Enabled = true;
    }

【问题讨论】:

  • e.IsTerminating 中的CurrentDomainUnhandledException 是什么?说我的应用程序仍然崩溃,你的意思是没有显示 MessageBox 或者 Memory 正在做的任何事情也会崩溃吗? -- 什么是e.Exception.AddLog()?你那里有另一个层(日志框架)来预处理异常吗? -- 我不会谈论你如何处理那里的Mutex :)

标签: c# winforms crash appdomain unhandled-exception


【解决方案1】:

尝试将[HandleProcessCorruptedStateExceptions] 属性添加到您的 Main 方法中。更重要的是,让你的主循环进入 try..catch:

[HandleProcessCorruptedStateExceptions]
static void Main(string[] args)
{
 //other code
  
  try
  {
    Application.Run(new MyApplicationContext());
  }catch(Exception)
  {
    //...
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 2010-09-09
    • 2021-08-27
    • 2019-04-09
    • 1970-01-01
    • 2021-11-13
    • 1970-01-01
    相关资源
    最近更新 更多