C# 如何以编程方式停止 Windows 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16317378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to stop Windows service programmatically
提问by roman_lenko
About programming Windows services: how to stop my windows service?
关于编程 Windows 服务:如何停止我的 Windows 服务?
Here is a very simplified example code(C#):
这是一个非常简化的示例代码(C#):
// Here is my service class (MyTestService.cs).
public class MyTestService:ServiceBase{
// Constructor.
public MyTestService(){
this.ServiceName = "My Test Service";
return;
}
};
// My application class (ApplicationClass.cs).
public static class ApplicationClass{
// Here is main Main() method.
public static void Main(){
// 1. Creating a service instance
// and running it using ServiceBase.
MyTestService service = new MyTestService();
ServiceBase.Run(service);
// 2. Performing a test shutdown of a service.
service.Stop();
Environment.Exit(0);
return;
};
};
So: I've just created "My Test Service" started it and stopped. But when I'm looking into my Services.msc - "My Test Service" is continues to running and stops ONLY when I click a "Stop" link. Why? - why service.Stop()command does nothing?
所以:我刚刚创建了“我的测试服务”,启动并停止了它。但是当我查看我的 Services.msc 时 - “我的测试服务”继续运行并且仅在我单击“停止”链接时停止。为什么?- 为什么service.Stop()命令什么都不做?
ServiceController.Stop() also does nothing!
ServiceController.Stop() 也什么都不做!
How can I stop my service from Main() method?
如何从 Main() 方法停止我的服务?
回答by Martin Mulder
The Stop-function sends a stop-signal. It does not wait till the signal is received and processed.
所述Stop-function发送一个停止信号。它不会等到信号被接收和处理。
You will have to wait till the Stop-signal has done it's work. You can do that by calling WaitForStatus:
您将不得不等到停止信号完成它的工作。你可以通过调用来做到这一点WaitForStatus:
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped);
See for more info: http://msdn.microsoft.com/nl-nl/library/system.serviceprocess.servicecontroller.waitforstatus(v=vs.71).aspx
有关更多信息,请参阅:http: //msdn.microsoft.com/nl-nl/library/system.serviceprocess.servicecontroller.waitforstatus(v=vs.71).aspx
Environment.Exitis a nasty one. DO NOT USE IT! It aborts your application the hard way, without performing any cleanup in finally blocks, without calling finalizer methods by the GC, it terminates all other forground threads, etc. I can imagine that your application is aborted before the stop-signal even left your application.
Environment.Exit是一个讨厌的。不要使用它!它以艰难的方式中止您的应用程序,无需在 finally 块中执行任何清理,无需通过 GC 调用终结器方法,它会终止所有其他前台线程等。我可以想象您的应用程序在停止信号甚至离开您的应用程序之前就已中止.
回答by Uzzy
I am using following functions in my project
我在我的项目中使用以下功能
public static ServiceController GetService(string serviceName)
{
ServiceController[] services = ServiceController.GetServices();
return services.FirstOrDefault(_ => Contracts.Extensions.CompareStrings(_.ServiceName, serviceName));
}
public static bool IsServiceRunning(string serviceName)
{
ServiceControllerStatus status;
uint counter = 0;
do
{
ServiceController service = GetService(serviceName);
if (service == null)
{
return false;
}
Thread.Sleep(100);
status = service.Status;
} while (!(status == ServiceControllerStatus.Stopped ||
status == ServiceControllerStatus.Running) &&
(++counter < 30));
return status == ServiceControllerStatus.Running;
}
public static bool IsServiceInstalled(string serviceName)
{
return GetService(serviceName) != null;
}
public static void StartService(string serviceName)
{
ServiceController controller = GetService(serviceName);
if (controller == null)
{
return;
}
controller.Start();
controller.WaitForStatus(ServiceControllerStatus.Running);
}
public static void StopService(string serviceName)
{
ServiceController controller = GetService(serviceName);
if (controller == null)
{
return;
}
controller.Stop();
controller.WaitForStatus(ServiceControllerStatus.Stopped);
}
回答by SerG
In your code example service.Stop()and ServiceController.Stop()commands does nothing because they are not called while service is running since ServiceBase.Run(service)is blocking operation and it returns only on stop of the service.
在您的代码示例中service.Stop(),ServiceController.Stop()命令不执行任何操作,因为它们在服务运行时不会被调用,因为它ServiceBase.Run(service)正在阻塞操作,并且它仅在服务停止时返回。

