C. Ross C. Ross - 1 month ago 13
C# Question

Handle exception on service startup

I'm writing a series of Windows services. I want them to fail if errors are thrown during startup (in

OnStart()
method). I had assumed that merely throwing an error in
OnStart()
would do this, but I'm finding that instead it "Starts" and presents me with a message stating "The service has started, but is inactive. Is this correct?" (Paraphrase). How do I handle the error so it actually fails to start the service?

Answer

if you are running .NET 2.0 or higher, you can use ServiceBase.Stop to stop the service from OnStart. Otherwise call Stop from a new thread.

ref [devnewsgroups] (http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework/topic50404.aspx)

(news group gone)

Comments