Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        /// <summary>
        /// 启动服务(启动存在的服务,60秒后启动失败报错)
        /// </summary>
        public void StartService()
        {
            DisableButtonStatus();
            int currentProgress = 0;

            LogTraceClient.WriteLog(LogTraceType.Info, "{0} 服务“{1}”启动开始".FormatString(DateTime.Now.ToString("HH:mm:ss fff"), GlobalParams.ServiceName));
            progressBar1.BeginInvoke(new EventHandler((o, e) =>
            {
                var progbar = (ProgressBar)o;

                progbar.Minimum = 0;
                progbar.Value   = 10;
                progbar.Maximum = 1000;
                currentProgress = progbar.Value;
            }));

            if (currentService != null)
            {
                if (currentService.Status != System.ServiceProcess.ServiceControllerStatus.Running && currentService.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
                {
                    try
                    {
                        //currentService.Start();

                        IServiceManage sm = new ServiceManage();
                        if (sm.StartService(serviceInfo))
                        {
                            LoggerFacade.Error("{0}服务启动异常".FormatString(serviceInfo.ServiceName));
                        }
                    }
                    catch (Exception ex)
                    {
                        LoggerFacade.Error("服务启动异常", ex);
                        return;
                    }
                    for (int i = 1; i <= 100; i++)
                    {
                        //this.progressBar1.Value = i * 10;
                        progressBar1.BeginInvoke(new EventHandler((o, e) =>
                        {
                            var progbar     = (ProgressBar)o;
                            progbar.Value   = i * 10;
                            currentProgress = progbar.Value;
                        }));

                        currentService.Refresh();
                        System.Threading.Thread.Sleep(100);
                        if (currentService.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                        {
                            progressBar1.BeginInvoke(new EventHandler((o, e) =>
                            {
                                var progbar     = (ProgressBar)o;
                                progbar.Value   = 1000;
                                currentProgress = progbar.Value;
                            }));
                            break;
                        }
                        if (i == 100)
                        {
                            System.Threading.Thread.Sleep(1000);
                            for (int k = currentProgress; k >= 0; k -= 20)
                            {
                                System.Threading.Thread.Sleep(100);
                                progressBar1.BeginInvoke(new EventHandler((o, e) =>
                                {
                                    var progbar = (ProgressBar)o;
                                    if (k < 0)
                                    {
                                        k = 0;
                                    }
                                    progbar.Value = k;
                                }));
                            }
                            currentProgress = 0;
                            // throw new Exception("服务" + serviceName + "启动失败!");
                            LogTraceClient.WriteLog(LogTraceType.Info, "{0} 服务“{1}”启动已超时".FormatString(DateTime.Now.ToString("HH:mm:ss fff"), GlobalParams.ServiceName));
                            LogTraceClient.WriteLog(LogTraceType.Info, "{0} 服务“{1}”启动异常,详细请看日志描述".FormatString(DateTime.Now.ToString("HH:mm:ss fff"), GlobalParams.ServiceName));
                            ChangeButtonStatus();
                            return;
                        }
                    }
                }
            }
            LogTraceClient.WriteLog(LogTraceType.Info, "{0} 服务“{1}”启动结束".FormatString(DateTime.Now.ToString("HH:mm:ss fff"), GlobalParams.ServiceName));

            ChangeButtonStatus();
        }