1

I used System.Threading.Timer in a Windows Service to do routine tasks. Will unhandled exceptions thrown in the callback method crash the service? I searched online, couldn't find any useful links.

I get this exception in the event log and the service crashes.

at MyNameSpace.MyClass.MyCallback(System.Object)
at System.Threading.ExecutionContext.runTryCode(System.Object)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode, CleanupCode, System.Object)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading._TimerCallback.PerformTimerCallback(System.Object)

Any MSDN article pointers on this will be appreciated.

ABC
  • 695
  • 1
  • 8
  • 18
  • 1
    This is something you can find out by yourself. Throw an exception in the callback and see what happens. – Marcom Feb 24 '14 at 12:37
  • Exceptions thrown (and not caught) in the callback method will happen on a different thread. Depending on how your application is written it could cause your service to crash. However, as the callback is not on the main thread it is likely the exception will just be swallowed or written to the event viewer. Might need to post some code for others to answer in more detail. Or search http://stackoverflow.com/questions/1718598/throwing-exceptions-in-callback-method-for-timers – MattC Feb 24 '14 at 12:37
  • Any reason to not to crash the service in case of unhandled exceptions? – Dennis Feb 24 '14 at 12:40

1 Answers1

1

From the point of exception handling, service behaves like any other application.
The same is true for the worker thread, which executes timer's callback.

There's no reason to continue application, if the exception was unhandled. The service will be terminated.

Dennis
  • 37,026
  • 10
  • 82
  • 150