private void Init() { this.currentService = new ServiceController(GlobalParams.ServiceName); IServiceManage sm = new ServiceManage(); ServiceState ss = sm.QueryService(new ServiceInfo() { ServiceName = GlobalParams.ServiceName }); if (ss == null) { this.currentService = null; } ChangeButtonStatus(); }
protected override void OnStart(string[] args) { //#if DEBUG // Debugger.Launch(); //Launches and attaches a debugger to the process. //#endif EventLog log = new EventLog("DSLOG"); if (!EventLog.SourceExists("DS.AFP.WindowsService")) { EventLog.CreateEventSource("DS.AFP.WindowsService", "DSLOG"); } log.Source = "DS.AFP.WindowsService"; log.WriteEntry("监控开始 OnStart", EventLogEntryType.Information); // TODO: 在此处添加代码以启动服务。 if (timer == null) { timer = new Timer(new TimerCallback((o) => { IServiceManage sm = new ServiceManage(); //ServiceInfo si = o as ServiceInfo; //if (sm.IsExistService(serviceInfo)) //{ // sm.CreateService(serviceInfo); //} ServiceState ss = sm.QueryService(serviceInfo); if (ss != null) { //如果状态停止则需要启动该服务 if (ss.State.IndexOf("1 STOPPED") != -1) { sm.StartService(serviceInfo); } } else { sm.CreateService(serviceInfo); } }), null, 100000, 12000); } base.OnStart(args); }