Floris Devriendt Floris Devriendt - 1 year ago 76
C# Question

The service could not be started, but does start?

I've written a small windows service that I want to run daily on my Windows Server 2008. The service is written in C#.

  • The code works great in a normal
    Windows Form.

  • The service works like it should when I start and stop it from the Services Management window (services.msc).

But when running it on a commandline with:

net start Service1

I get the the following:

The Service1 service is starting........ The Service1 service could not be started.

The service did not report an error.

More help is available by typing NET HELP:SG 3534.

The weird thing is though, the service is still running, in the Services screen I still see it as starting untill it's fully started. When I try to stop the service afterwards I get:

The service could not be controlled in its present state.

More help is available by typing NET HELPMSG 2189

And then the service is stopped. Is there any way in solving this? I've already managed to debug the service without any problems, the code works. But during debugging the same thing still happens on the command console, while I'm still able to debug further.

It's like there is some kind of timeout on the onStart() method.. I have no idea.. I'm fairly new to Windows Services (this is my first one). I do write all my code in the onStart() method, maybe that's not the best idea, but I don't know where else to type it.

If someone could help I'd greatly appreciate it.

Answer Source

The OnStart event is used to launch a background thread that will take care of the processing. If the OnStart method finishes without error, the service manager assumes the service started successfully. As such, the OnStart should return as soon as possible.

The OnStop method is then used to stop the background processing. A successful OnStop tells the service manager that the service was closed without error.