views:

1274

answers:

3

I have deployed my windows service (using independently installer class and SC.EXE), but I get an error when I try to start it:

---------------------------
Services
---------------------------
Could not start the MyName service on Local Computer.



Error 1053: The service did not respond to the start or control request in a timely fashion.

What is the timeout? It felt like around 3 secs. What do I need to do if my service takes longer?

+5  A: 

The normal way of creating a service is to have the startup code create a new thread, and run your service in that thread.

The service startup should be nearly instantaneous - nothing more than spawning a new thread with your "real" work in it.

If you're taking more than three seconds, that's a sign that you're doing the real work in your main thread, and not creating a separate one for your service.

Reed Copsey
This assumes poor design where it might not be present. Complex initialization can take extra time, and the "failed to respond" message alerts users to a failed startup. If the failure occured in a thread and the service stops as a result, the only visible evidence (without a separate monitoring UI) will be in the event log.
Ben M
why? !
zvolkov
There is a reason there is a 3 second time limit for startup. There are ways to extend this (RequestAdditionalTime()), but the recommendation is that services should start promptly, and/or push the work into a separate thread, and alert in a different manner. Taking a long time for startup will prevent other services from starting promptly, which IS a bad design, IMO.
Reed Copsey
That's simply didactic.
Ben M
This design is a bit weird - why would I pretend my service can start in 3 sec, when it actually fails in the 4th second?
Grzenio
I'd have no problem with requesting 4 seconds to start - but I would have a problem with a service that requested 3 minutes to start. I'd uninstall that software, since it'd pretty much kill startup times for the system...
Reed Copsey
+5  A: 

In your service class, use ServiceBase.RequestAdditionalTime() in your OnStart/OnStop method:

// request an additional 4 seconds to complete the operation
RequestAdditionalTime(4000);
Ben M
A: 

In regards to the specific question, the exact timeout varies, but is less than 30 seconds. You can control the default startup timeout for a service via a registry key, you can see how to do this here.

However, I will agree with many others that I would look at two possible options.

  1. Get your service started ASAP, spawn a thread, etc..
  2. If you cannot go with option one, you can use RequestAdditionalTime(). Just be sure to make this call early on.
Mitchel Sellers