我想停止 Windows 服务onStart()
客户没有许可证时的方法。我用service.Stop()
,但它不起作用。
protected override void OnStart(string[] args)
{
try
{
_bridgeServiceEventLog.WriteEntry("new OnStart");
if (LicenseValidetor.ValidCountAndTypeDevices())
{
WsInitializeBridge();
}
else
{
service = new ServiceController("BridgeService");
service.Stop();
_bridgeServiceEventLog.WriteEntry("LicenseValidetor Error");
}
_bridgeServiceEventLog.WriteEntry("end Start");
}
catch (Exception e)
{
_bridgeServiceEventLog.WriteEntry("error In onstart method ");
}
}
您无法从以下位置停止服务OnStart
相同服务的方法。
The ServiceController.Stop http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicecontroller.stop.aspx方法内部调用ControlService http://msdn.microsoft.com/en-us/library/windows/desktop/ms682108%28v=vs.85%29.aspx(或者它是Ex
对方)。请注意,此函数可能失败的原因之一是:
ERROR_SERVICE_CANNOT_ACCEPT_CTRL请求的控制代码无法发送到服务,因为服务的状态为服务已停止, SERVICE_START_PENDING, or SERVICE_STOP_PENDING.
好吧,猜猜看 - 当你在你的OnStart
方法,你的服务状态是SERVICE_START_PENDING
.
处理这种情况的正确方法是向任何其他线程发出信号,表明您可能已开始让它们退出,然后退出您的线程。OnStart
方法。服务控制管理器将注意到该进程已退出并将您的服务状态恢复为SERVICE_STOPPED
。它还可能通知交互式用户“服务启动然后停止”或类似的文字。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)