【发布时间】:2017-02-22 18:34:22
【问题描述】:
我有一个运行 BackgroundWorker 的简单 WinForms 应用程序。
下面是我附加异常处理程序的主要位置。
static class Program
{
private static Logger _logger = LogManager.GetCurrentClassLogger();
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.ThreadException += OnThreadException;
//Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
// AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
try
{
Application.Run(new Form());
}
catch (Exception exc)
{
LogFatalException(exc);
}
}
private static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (e.IsTerminating)
{
_logger.Info("Application is terminating due to an unhandled exception in a secondary thread.");
}
LogFatalException(e.ExceptionObject as Exception);
}
private static void OnThreadException(object sender, ThreadExceptionEventArgs e)
{
LogFatalException(e.Exception);
}
private static void LogFatalException(Exception exc)
{
_logger.Error(exc, exc.Message);
MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
我正在调用我的 BackgroundWorker,如下所示。我希望后台线程上发生的任何类型的异常都会冒泡到主要处理方法和日志/警报,但不关闭应用程序。
我做错了什么?应用程序仍在关闭中。
backgroundWorker1.RunWorkerCompleted += (senderWorker, eWorker) =>
{
if (eWorker != null)
{
throw new Exception("Error Processing. Please check connection and try again.");
}
};
backgroundWorker1.RunWorkerAsync();
【问题讨论】:
-
如果您没有在某处捕获该异常,框架将终止应用程序。
-
彼得,你能根据我列出的代码更具体吗?我应该在哪里发现异常?
-
为什么要注释 UnhandledException 订阅?
-
为什么要抛出异常?在您处于 UI 线程的 Completed 事件中,只需使用任何错误信息更新 UI。
-
eWorker 将是一个 RunWorkerCompletedEventArgs 对象。我无法想象它永远是空的。
标签: c# multithreading winforms backgroundworker