如果您正在寻找非 UI 方法(例如,将名称写入文件或标准输出),则可以在服务上使用 ExecuteCommand 方法。
ServiceController sc = new ServiceController("ServiceName");
sc.ExecuteCommand(255);
Run Code Online (Sandbox Code Playgroud)
这只是将此命令传递给您的服务,您的服务将通过 OnCustomCommand 处理它
protected override void OnCustomCommand(int command)
{
base.OnCustomCommand(command);
if (command == 255
{
... // Your code here
}
}
Run Code Online (Sandbox Code Playgroud)
您可能需要将队列/服务状态存储在静态变量中,以便可以从 OnCustomCommand 例程访问它。
| 归档时间: |
|
| 查看次数: |
1411 次 |
| 最近记录: |