关于编写Windows服务:如何停止我的Windows服务?
这里是一个非常简单的示例代码(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;
};
};
所以:我刚刚创建了“我的测试服务”,启动并停止了它。但是当我查看我的Services.msc时,“我的测试服务”仍在运行,并且只有在我点击“停止”链接时才会停止。为什么?-为什么service.Stop()命令没有任何作用?
ServiceController.Stop()也没有任何作用!
如何从Main()方法停止我的服务?