Windows Service 经验总结 1 --- Service Control Manager

本文总结了Windows服务的启动超时问题,介绍了如何使用Service Control Manager延长服务启动时间限制,并提供了一个批处理文件SvcTimeOut.bat来调整超时值。此外,还讨论了在服务的"OnStart"方法中创建初始化线程来处理耗时启动任务的策略,以避免30秒的时间限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Service Startup

http://msdn.microsoft.com/en-us/library/ms685990(VS.85).aspx

 

2. A way to prolong start time limit of Windows services in Service Control Manager

When a service fails to send a 'service started' message to the Service Control Manager within the time-out period, the Service Control Manager terminates the service and logs Event ID 7000 to the System event log.

 

NOTE: The default timeout period is normally 30,000 milliseconds.

I have scripted SvcTimeOut.bat to alter the time that the Service Control Manager wait for each starting service.

The syntax for using SvcTimeOut.bat is:

        call SvcTimeOut TimeOutMilliseconds

Where TimeOutMilliseconds is the number of milliseconds that the Service Control Manager should wait.

NOTE:

You must shutdown and restart your computer for the new setting to become effective.

NOTE:

SvcTimeOut.bat uses REG.EXE, built into Windows XP, Windows Server 2003, and later, or installed from the Windows 2000 Support Tools.

 

SvcTimeOut.bat contains:

@echo off

if {%1}=={} @echo Syntax: SvcTimeOut TimeOutMilliseconds&goto :EOF

@echo REG ADD "HKLM/SYSTEM/CurrentControlSet/Control" /V ServicesPipeTimeout /T REG_DWORD /F /D %1

REG ADD "HKLM/SYSTEM/CurrentControlSet/Control" /V ServicesPipeTimeout /T REG_DWORD /F /D %1

 

3. How to deal with time consumering start up service ?

A common practice with Windows Services is to create an initialization thread in the "OnStart" method of the service. The thread can do the time consuming start up work. This way, you are guaranteed to have your service start up instantly, and not hit the default 30 second time limit. If for whatever reason something goes wrong with the initialization thread, you can always shut the service down, log errors to the event log, etc.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值