ronmar
ronmar

Reputation: 39

How to know if a service is still running after starting it

I want to determine if the service I started is actually started and not in starting status... How can I know if the service is still running after I start it? Maybe get the event that it started or something like that...

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

Upvotes: 0

Views: 509

Answers (1)

user4628565
user4628565

Reputation:

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";
}

or you can check,

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

Upvotes: 1

Related Questions