Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the Exception that happens in Timer Elapsed event?

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);         }     } 
like image 398
Clack Avatar asked Sep 20 '10 08:09

Clack


People also ask

How do I stop a timer elapsed event?

It would have already queued before you have called Stop method. It will fire at the elapsed time. To avoid this happening set Timer. AutoReset to false and start the timer back in the elapsed handler if you need one.

What is timer Elapsed event in Windows service?

Elapsed event every two seconds (2000 milliseconds), sets up an event handler for the event, and starts the timer. The event handler displays the value of the ElapsedEventArgs. SignalTime property each time it is raised.

What does timer elapsed mean?

Elapsed time is the amount of time that passes from the start of an event to its finish. In simplest terms, elapsed time is how much time goes by from one time (say 3:35pm) to another (6:20pm).


2 Answers

If you don't have access to the main thread, you can throw the exception on another, non-timer thread:

catch (Exception exception) {     ThreadPool.QueueUserWorkItem(         _ => { throw new Exception("Exception on timer.", exception); }); } 
like image 187
svick Avatar answered Oct 01 '22 13:10

svick


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 via Control.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:

In the .NET Framework version 2.0 and earlier, the Timer component catches and suppresses all exceptions thrown by event handlers for the Elapsed event. This behavior is subject to change in future releases of the .NET Framework.

Just checked in .NET 4.0, and this behavior has not yet changed.

like image 32
Kent Boogaart Avatar answered Oct 01 '22 12:10

Kent Boogaart