简体   繁体   English

如果正在运行,请先停止Windows服务,然后再从安装程序C#中再次运行

[英]Stop windows service if running before run again from installer c#

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. 我已经为Windows服务创建了安装程序,并且从AfterInstall事件中启动了服务,但是在启动服务之前,我想检查服务是否未运行,然后我将启动服务,否则不会启动,但是我不知道如何检查我的服务是否正在运行或不是来自AfterInstall事件。 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();
        }
    }
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM