如何知道服务在启动后是否仍在运行

本文关键字:是否 运行 启动 何知道 服务 | 更新日期: 2023-09-27 18:09:55

我想确定我启动的服务是否实际启动,而不是处于启动状态…我如何知道服务在启动后是否仍在运行?也许得到它开始的事件或类似的东西…

using System.ServiceProcess;  
ServiceController sc = new ServiceController(SERVICENAME);
sc.start();

如何知道服务在启动后是否仍在运行

try,

using System.ServiceProcess;
ServiceController sc = new ServiceController(SERVICENAME);
switch (sc.Status)
{
case ServiceControllerStatus.Running:
    return "Running";
case ServiceControllerStatus.Stopped:
    return "Stopped";
case ServiceControllerStatus.Paused:
    return "Paused";
case ServiceControllerStatus.StopPending:
    return "Stopping";
case ServiceControllerStatus.StartPending:
    return "Starting";
default:
    return "Status Changing";
}

或者你可以检查

ServiceController[] services = ServiceController.GetServices();
foreach(ServiceController service in services)
{
Console.WriteLine(service.ServiceName+"=="+ service.Status);
}