C# 启动 Windows 服务的超时时间是多少?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1160295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the timeout for starting a windows service?
提问by Grzenio
I have deployed my windows service (using independently installer class and SC.EXE), but I get an error when I try to start it:
我已经部署了我的 Windows 服务(使用独立的安装程序类和 SC.EXE),但是当我尝试启动它时出现错误:
---------------------------
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?
什么是超时?感觉好像是3秒左右。如果我的服务需要更长的时间,我需要做什么?
采纳答案by Ben M
In your service class, use ServiceBase.RequestAdditionalTime()
in your OnStart/OnStop method:
在您的服务类中,ServiceBase.RequestAdditionalTime()
在您的 OnStart/OnStop 方法中使用:
// request an additional 4 seconds to complete the operation
RequestAdditionalTime(4000);
回答by Reed Copsey
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.
如果您花费的时间超过三秒钟,则表明您正在主线程中进行真正的工作,而不是为您的服务创建单独的工作。
回答by Mitchel Sellers
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.
关于具体问题,确切的超时时间各不相同,但都小于 30 秒。您可以通过注册表项控制服务的默认启动超时,您可以在此处查看如何执行此操作。
However, I will agree with many others that I would look at two possible options.
但是,我会同意许多其他人的看法,即我会考虑两种可能的选择。
- Get your service started ASAP, spawn a thread, etc..
- If you cannot go with option one, you can use RequestAdditionalTime(). Just be sure to make this call early on.
- 尽快启动您的服务,生成线程等。
- 如果您不能使用选项一,您可以使用 RequestAdditionalTime()。请务必尽早拨打此电话。
回答by wacdany
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.
此外,如果您在不同的物理环境中测试了该服务,那么问题似乎不是正常启动时间,而是 PC 的性能。您可以增加特定 PC 的注册表项的超时时间。
Please see: http://support.microsoft.com/kb/839803
请参阅:http: //support.microsoft.com/kb/839803
Regards
问候