コード例 #1
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();
        }
コード例 #2
0
        /// <summary>
        /// 停止服务
        /// </summary>
        /// <param name="serviceName">服务名</param>
        public void StopService()
        {
            DisableButtonStatus();

            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   = 0;
                progbar.Maximum = 1000;
            }));

            if (currentService != null)
            {
                if (currentService.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                {
                    System.DateTime startTime = System.DateTime.Now;

                    IServiceManage sm = new ServiceManage();

                    if (sm.StopService(serviceInfo))
                    {
                        LoggerFacade.Error("{0}服务停止".FormatString(serviceInfo.ServiceName));
                    }
                    //bool isExecute = true;
                    //while (isExecute)
                    //{
                    //    if ((System.DateTime.Now - startTime).TotalSeconds < 120)
                    //    {
                    //        if (currentService.CanStop)
                    //        {
                    //            currentService.Stop();
                    //            isExecute = false;
                    //        }
                    //        currentService.Refresh();

                    //    }
                    //    else
                    //    {
                    //        LogTraceClient.WriteLog(LogTraceType.Info, "{0} 服务“{1}”停止失败,已经超时".FormatString(DateTime.Now.ToString("HH:mm:ss fff"), GlobalParams.ServiceName));

                    //    }
                    //}

                    bool isExecute2 = true;
                    while (isExecute2)
                    {
                        if ((System.DateTime.Now - startTime).TotalSeconds < 120)
                        {
                            if (currentService.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
                            {
                                progressBar1.BeginInvoke(new EventHandler((o, e) =>
                                {
                                    var progbar   = (ProgressBar)o;
                                    progbar.Value = 970;
                                }));

                                Process[] serviceProcess = System.Diagnostics.Process.GetProcesses();
                                int       count          = 0;
                                foreach (var p in serviceProcess)
                                {
                                    if (p.Id != 0)
                                    {
                                        if (p.ProcessName == Program.HOSTNAME)
                                        {
                                            count++;
                                        }
                                    }
                                }
                                if (count == 1)
                                {
                                    isExecute2 = false;
                                }
                            }
                            else
                            {
                                sm.StopService(serviceInfo);
                                Thread.Sleep(1000);
                                progressBar1.BeginInvoke(new EventHandler((o, e) =>
                                {
                                    var progbar = (ProgressBar)o;
                                    if (progbar.Value <= 940)
                                    {
                                        progbar.Value += 30;
                                    }
                                }));
                            }

                            currentService.Refresh();
                        }
                        else
                        {
                            isExecute2 = false;
                            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));
            progressBar1.BeginInvoke(new EventHandler((o, e) =>
            {
                var progbar   = (ProgressBar)o;
                progbar.Value = 1000;
            }));
            ChangeButtonStatus();
        }
コード例 #3
0
        public void RegisterService()
        {
            DisableButtonStatus();

            IServiceManage sm = new ServiceManage();

            //this.progressBar1.Minimum = 0;
            //this.progressBar1.Value = 0;
            //this.progressBar1.Maximum = 1000;
            progressBar1.BeginInvoke(new EventHandler((o, e) =>
            {
                var progbar = (ProgressBar)o;

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

            if (currentService != null)
            {
                // Thanks to PIEBALDconsult's Concern V2.0

                LogTraceClient.WriteLog(LogTraceType.Info, "{0} 卸载服务“{1}”开始".FormatString(DateTime.Now.ToString("HH:mm:ss fff"), GlobalParams.ServiceName));

                if (sm.DeleteService(serviceInfo))
                {
                    progressBar1.BeginInvoke(new EventHandler((o, e) =>
                    {
                        var progbar = (ProgressBar)o;

                        for (int i = 0; i < 1000; i++)
                        {
                            progbar.Value++;
                        }
                    }));

                    currentService = null;
                    IsExsitService = false;
                    ChangeButtonStatus();

                    LogTraceClient.WriteLog(LogTraceType.Info, "{0} 服务“{1}”已经成功卸载".FormatString(DateTime.Now.ToString("HH:mm:ss fff"), GlobalParams.ServiceName));
                }
                else
                {
                    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));
            }
            else
            {
                LogTraceClient.WriteLog(LogTraceType.Info, "{0} 安装服务“{1}”开始".FormatString(DateTime.Now.ToString("HH:mm:ss fff"), GlobalParams.ServiceName));


                if (sm.CreateService(serviceInfo))
                {
                    progressBar1.BeginInvoke(new EventHandler((o, e) =>
                    {
                        var progbar = (ProgressBar)o;

                        for (int i = 0; i < 1000; i++)
                        {
                            progbar.Value++;
                        }
                    }));

                    currentService = new System.ServiceProcess.ServiceController(GlobalParams.ServiceName);
                    IsExsitService = true;
                    ChangeButtonStatus();

                    LogTraceClient.WriteLog(LogTraceType.Info, "{0} 服务“{1}”已经注册成功".FormatString(DateTime.Now.ToString("HH:mm:ss fff"), GlobalParams.ServiceName));
                }
                else
                {
                    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));
            }
        }