我正在尝试使用我编写的控制台程序停止正在运行的服务。 这是我的代码:
ServiceController sc = new ServiceController(ServiceName, ".");
sc.Stop();
然后我等待服务状态变为已停止,但这从未发生过。 它似乎有 StopPending 并且永远不会停止。 当我检查 Services.msc 时,它被列为已停止,并且在任务管理器中,该进程已消失。
它会一直处于 Pending 状态吗?难道我做错了什么? 如果我需要在那里修改某些内容,我也有该服务的代码。
最佳答案
Am I doing something wrong?
你可以试试waiting停止服务后:
sc.Stop();
sc.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(25));
WaitForStatus
方法将等待服务达到指定的状态或指定的超时时间到期后返回。
关于c# - 停止服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10480914/