【发布时间】: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