我正在使用Windows窗体应用程序,并使用System.Timers.Timer来定期检查数据库中的数据。如何获取Timer Elapsed事件中发生的异常?
如何获取发生在计时器中的异常发送到主应用程序中的事件处理程序?如果我使用下面的代码,异常get会被“吞下去”,并且主应用程序永远不会得到它(即使我有处理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);
}
}
代码示例请问? – hrh 2012-04-25 16:31:03
我想我得到它,我将异常传递给我的主线程上的函数,然后该函数在抛出异常之前在函数中使用this.Invoke()。我是否正确理解这一点? – hrh 2012-04-25 16:41:43
是否有任何新的方法来捕获.net 4.5的意外错误?它仍然吞咽,并不是无论如何记录错误没有包装整个_timer_Elapsed里面试试catch? – MonsterMMORPG 2014-08-21 13:02:49