[英]ExecuteCommand servicecontroller - cannot excecute command on service
我正在使用服务 controller 执行命令 function,如下所示:
ServiceController serviceController = new ServiceController("a Service",
Environment.MachineName);
serviceController.ExecuteCommand(129);
并在服务 controller 中:
protected override void OnCustomCommand(int command)
{
base.OnCustomCommand(command);
// Depending on the integer passed in, the appropriate method is called.
switch (command)
{
case 129:
RestartSpooler();
break;
case 131:
InstallPrinter();
break;
case 132:
DeletePrinter();
break;
}
}
然而,尽管从调用代码中调用了任何命令(代码命中该行,然后跳过,没有异常),但什么也没有发生。 为什么? 这一切都在本地机器上,我拥有完全的管理员权限。
谢谢
您必须尝试对已停止的服务执行命令。 添加如下内容:
if (serviceController1.Status == ServiceControllerStatus.Stopped)
{
serviceController1.Start();
}
serviceController1.ExecuteCommand(192);
我还没有找到它不应该工作的任何理由。 这是使用自定义命令的 windows 服务的工作示例
public partial class TestService : ServiceBase
{
public TestService()
{
InitializeComponent();
}
protected override void OnStart(string[] args) { }
protected override void OnStop() { }
protected override void OnCustomCommand(int command)
{
base.OnCustomCommand(command);
switch (command)
{
case 129:
//
break;
case 131:
//
break;
case 132:
//
break;
}
}
}
服务安装程序
[RunInstaller(true)]
public partial class Installer : System.Configuration.Install.Installer
{
public Installer()
{
InitializeComponent();
_processInstaller = new ServiceProcessInstaller();
_processInstaller.Account = ServiceAccount.LocalSystem;
_serviceInstaller = new ServiceInstaller();
_serviceInstaller.StartType = ServiceStartMode.Manual;
_serviceInstaller.ServiceName = "TestService";
Installers.Add(_serviceInstaller);
Installers.Add(_processInstaller);
}
private readonly ServiceInstaller _serviceInstaller;
private readonly ServiceProcessInstaller _processInstaller;
}
服务使用
var serviceController = new ServiceController("TestService", Environment.MachineName);
serviceController.ExecuteCommand(129);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.