2014-09-03 75 views
0

我创建的Windows安装程序服务,并从AfterInstall事件我开始服务,但开始前我要检查,如果服务没有那么运行的服务安装程序再次运行之前,我会以其他方式启动服务不,但我不知道如何检查我的服务正在运行或不从AfterInstall事件。请指导。感谢停止Windows服务,如果运行在C#

using System.ServiceProcess; 
class ServInstaller : ServiceInstaller 
{ 
    void ServiceInstaller_AfterInstall(object sender, InstallEventArgs e) 
    { 
     using (ServiceController sc = new ServiceController(serviceInstaller1.ServiceName)) 
     { 
      sc.Start(); 
     } 
    } 
} 
+0

的[我如何可以验证,如果Windows服务运行]可能重复(http://stackoverflow.com/questions/178147 /如何-可以-I-验证-IF-A-Windows的服务是运行) – Noctis 2014-09-03 08:48:10

+0

看一看的链接,它表明你如何检查它是否运行 – Noctis 2014-09-03 08:48:35

回答

0

你可以检查一下,如下图所示: -

using (ServiceController sc = new ServiceController(serviceInstaller1.ServiceName)) 
{  
    if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) || 
    (sc.Status.Equals(ServiceControllerStatus.StopPending))) 
    { 
    // Your code when service stopped 
    } 
}