Force Windows Service to stop running after exception is handled

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!
 
J

Jay B. Harlow [MVP - Outlook]

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
 
D

David Hunt

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top