【发布时间】:2015-06-09 19:28:21
【问题描述】:
我正在使用 Windows 窗体应用程序,并且有一个管理器类,它使用 System.Timers.Timer 定期检查数据库中的数据。
如何将计时器 Elapsed 事件处理程序中发生的异常传递到主应用程序?如果我使用下面的代码,异常会被“吞下”,而主应用程序永远不会得到它(即使我有 ThreadException 和 UnHandledException 的处理程序)。
// Main Form
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
// Manager class
private System.Timers.Timer _timer;
void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
doSomeDatabaseActions();
}
catch (Exception ex)
{
throw new ApplicationException("How do I get this error back into main thread...", ex);
}
}
【问题讨论】:
标签: c# multithreading timer