如何获取 Timer Elapsed 事件中发生的异常?
我正在使用 Windows 窗体应用程序,并且有一个管理器类,它使用 System.Timers.Timer 定期检查数据库中的数据。
如何获取传递到主应用程序的计时器已用事件处理程序中发生的异常?如果我使用下面的代码,异常将被“吞没”,并且主应用程序永远不会获取它(即使我有 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);
}
}
I'm working with Windows Forms application and hava a manager class that uses System.Timers.Timer to periodly check data from database.
How do I get the Exception that occurs in timer Elapsed eventhandler delivered into main application? If I'm using the code below, the exception get's "swallowed", and main application never gets it (even if I have handlers for ThreadException and 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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您无权访问主线程,则可以在另一个非计时器线程上抛出异常:
If you don't have access to the main thread, you can throw the exception on another, non-timer thread:
由于 System.Timers.Timer 会吞掉事件处理程序中引发的任何异常,因此您需要将异常封送到另一个线程(可能是 UI 线程)。您可以通过 Control.Invoke 来完成此操作,或者通过将错误信息存储在成员变量中并让 UI 线程在操作完成后检查此错误信息。如果非
null
,则 UI 可能会抛出异常。来自 MSDN:
刚刚签入.NET 4.0,这种行为尚未改变。
Since
System.Timers.Timer
swallows any exception thrown in the event handler, you will need to marshal the exception to another thread (probably the UI thread). You could do this viaControl.Invoke
, or by storing error information in a member variable and having the UI thread check this error information after the operation is complete. If non-null
, the UI could then throw.From MSDN:
Just checked in .NET 4.0, and this behavior has not yet changed.
您可以将异常分配给局部变量并检查是否抛出异常:
You can assign the exception to a local variable and check if an exception has been thrown:
我猜你想处理主窗体上的异常,这个解决方案并不完整,但展示了如何使用“操作”来做到这一点
I guess that you want to handle the exception on the main form, this solution is not complete but shows how to do it with an "Action"