0

i have created installer for windows service and from AfterInstall event i am starting service but before start the service i like to check if service is not running then i will start the service otherwise not but i do not know how to check my service is running or not from AfterInstall event. please guide. thanks

using System.ServiceProcess;  
class ServInstaller : ServiceInstaller
{
    void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e)
    {
        using (ServiceController sc = new ServiceController(serviceInstaller1.ServiceName))
        {
            sc.Start();
        }
    }
}
Thomas
  • 31,089
  • 118
  • 335
  • 589
  • possible duplicate of [How can I verify if a Windows Service is running](http://stackoverflow.com/questions/178147/how-can-i-verify-if-a-windows-service-is-running) – Noctis Sep 03 '14 at 08:48
  • Have a look at the link, it shows you how to check if it's running – Noctis Sep 03 '14 at 08:48

1 Answers1

1

You can check it as shown below :-

using (ServiceController sc = new ServiceController(serviceInstaller1.ServiceName))
{    
   if  ((sc.Status.Equals(ServiceControllerStatus.Stopped)) ||
     (sc.Status.Equals(ServiceControllerStatus.StopPending)))
   {
   // Your code when service stopped
   }  
}
Neel
  • 11,231
  • 3
  • 41
  • 59