What is the timeout for starting a windows service

2019-02-21 15:57发布

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?

4条回答
霸刀☆藐视天下
2楼-- · 2019-02-21 16:16

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.
查看更多
太酷不给撩
3楼-- · 2019-02-21 16:23

Also if you had tested the service in different Physical environments, and it seems that the issue is not the normal startup time but the performance of the PCs. You can increase the timeout on the registry key for the specific PC.

Please see: http://support.microsoft.com/kb/839803

Regards

查看更多
等我变得足够好
4楼-- · 2019-02-21 16:28

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.

查看更多
Evening l夕情丶
5楼-- · 2019-02-21 16:34

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

// request an additional 4 seconds to complete the operation
RequestAdditionalTime(4000);  
查看更多
登录 后发表回答