我正在使用以下代码处理程序中的所有未处理异常。但问题在于异常没有传播到指定的方法。
请指导我在这里做错了什么,我希望我的程序中任何地方生成的异常都能被捕获到上面的全局处理程序中,以便将错误/异常报告回来进行修复。
谢谢。
[STAThread]
static void Main()
{
AppDomain currentDomain = default(AppDomain);
currentDomain = AppDomain.CurrentDomain;
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
// Handler for unhandled exceptions.
currentDomain.UnhandledException += GlobalUnhandledExceptionHandler;
// Handler for exceptions in threads behind forms.
System.Windows.Forms.Application.ThreadException += GlobalThreadExceptionHandler;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormMain());
}
private static void GlobalUnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e)
{
Exception ex = (Exception)e.ExceptionObject;
MessageBox.Show(ex.Message,
"Important",
MessageBoxButtons.YesNo);
}
private static void GlobalThreadExceptionHandler(object sender, System.Threading.ThreadExceptionEventArgs e)
{
Exception ex = e.Exception;
MessageBox.Show(ex.Message,
"Important",
MessageBoxButtons.YesNo);
}
FormMain有一个backgroundWorker对象。这个backgroundworker是应用程序的主要部分,如果doWork方法中出现异常,它不会按照我期望的那样传播..以下是我在MainForm中使用的backgroundworker的代码。
private void button_startSync_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync(getSettings());\
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// ... processing code
// Unhandled Exception will generated in this method...
}
请指导我在这里做错了什么,我希望我的程序中任何地方生成的异常都能被捕获到上面的全局处理程序中,以便将错误/异常报告回来进行修复。
谢谢。