Force Windows Service to stop running after exception is handled

  • Thread starter Thread starter David Hunt
  • Start date Start date
D

David Hunt

I'm developing a Windows Service and just added a standard try... catch
exception handler to its OnStart() routine. I would like to know how to
force the service to abort its startup when an exception is encountered. In
my current version, the exception handler logs the exception, but then the
service continues to run. I'd like it to NOT run after logging the
exception.

Thanks!
 
David,
The "easiest" way is to remove the try/catch in the OnStart routine, or at
least include throw in the catch clause...

For if the OnStart throws an exception ServiceBase does not allow the
service to start & if AutoLog is True, logs the fact it didn't start for
you. I don't remember if it logs the exception details, which can be
handy...

Hope this helps
Jay
 
Thanks. I think I'll log the Exception explicitly and then throw a new one
just to force it to not start (I need both to happen; log exception +
service NOT start).
 
Back
Top