ServiceController.Stop() fails to stop service

↘锁芯ラ 提交于 2019-12-25 03:36:12

问题


I have a windows service which I'm trying to stop progrmatically using ServiceController.Stop():

ServiceController sc = new ServiceController(<ServiceName>);                    
sc.Stop();
sc.Refresh();

However, when I see this service in the Services list, its status still remains as "Started". If I call the method sc.WaitForStatus() to wait till the status is changed to "Stopped", then this status is never reached.

What's going wrong?


回答1:


I think problem was with legacy code.

We need some configuration and the requirement is if configuration is missing in config file, service should not be started. This validation was done in OnStart(). The ServiceController.Stop() was called from the OnStart() method itself whenever validation fails! I think that's why ServiceController.Stop() was not able to stop the service and I never got the status "Stopped" in WaitForStatus() method call.

I changed the logic and now i dont call ServiceBase.Run() method (which gives call to OnStart()) if necessary configuration is missing.

The legacy code was trying to Stop the service from its Start method!



来源:https://stackoverflow.com/questions/4813948/servicecontroller-stop-fails-to-stop-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!