Windows 服务,不能启动的浅层推测

本文详细解析了Windows服务中的多线程机制,解释了为何有时服务无法正常停止或启动,并提供了重启这一简单而有效的方法来解决这类问题。同时,文章还分享了使用工具如ProcessMonitor或ProcessExplorer来杀死线程的解决方案,帮助开发者更有效地管理服务状态。
大家可能都遇到过windows某个服务无法操作的情况,无法停止或者无法启动的现象。
原来对Windows服务的本质不太了解,今天我想我可能切中要害了。先看下文,再听我给您讲。希望大家指出不对之处。
Multithreaded Services

The service control manager (SCM) controls a service by sending service control events to the service's control handler routine. The service must respond to control events in a timely manner so that the SCM can keep track of the state of the service. Also, the state of the service must match the description of its state that the SCM receives.

Due to this communication mechanism between a service and the SCM, you must be careful when using multiple threads in a service. When a service is instructed to stop by the SCM, it must wait for all the threads to exit before reporting to the SCM that the service is stopped. Otherwise, the SCM can become confused about the state of the service and might fail to shut down correctly.

The SCM needs to be notified that the service is responding to the stop control event and that progress is being made in stopping the service. The SCM will assume the service is making progress if the service responds (through SetServiceStatus) within the time (wait hint) specified in the previous call to SetServiceStatus, and the check point is updated to be greater than the checkpoint specified in the previous call to SetServiceStatus.

If the service reports to the SCM that the service has stopped before all threads have exited, it is possible that the SCM will interpret this as a contradiction. This might result in a state where the service cannot be stopped or restarted.

 

原来可能是多线程服务的代码设计的不周全,导致上文中红色部分描述的情况出现了。

那也就说明了,为什么有的时候重启之后就好了(操作系统重启,原来未退出的进程自然早没了,问题自然也就没了。)

 

因为,作为程序员,我想我也找到了相关的解决办法,用工具(Process Monitor或者ProcessExplorer)杀死线程,不过这可不是件容易干的活,对一般人来说,还是重启简单稳当点。

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值