Exemplo n.º 1
0
 void onServiceStateChange(SmServiceEventArgs e)
 {
     if (this.OnServiceStateChange != null)
     {
         this.OnServiceStateChange(this, e);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// 重启服务
        /// </summary>
        /// <param name="serviceName">服务名</param>
        /// <returns></returns>
        public bool ResetService(string serviceName)
        {
            bool isOK             = false;
            SmServiceEventArgs es = new SmServiceEventArgs();

            if (IsExisted(serviceName))
            {
                es.ServiceName   = serviceName;
                es.ServiceOption = ServiceOptions.Reset;
                es.CompleteState = ServiceCompleteStateType.Trying;
                onServiceStateChange(es);

                if (StopService(serviceName))
                {
                    if (StartService(serviceName))
                    {
                        isOK = true;
                    }
                }

                //es.ServiceOption = ServiceOptions.Reset;
                //es.Error = !isOK;
                //if (!isOK) { es.CompleteState = ServiceCompleteStateType.Successfully; }
                //else { es.CompleteState = ServiceCompleteStateType.Error; }
                //es.Error = !isOK;
                //onServiceStateChange(es);
            }
            else
            {
                es.ServiceName   = serviceName;
                es.ServiceOption = ServiceOptions.Reset;
                es.CompleteState = ServiceCompleteStateType.NoExist;
                es.Error         = new Exception("指定的服务不存在");
                onServiceStateChange(es);
            }

            return(isOK);
        }
Exemplo n.º 3
0
 void onServiceStateChange(SmServiceEventArgs e)
 {
     if (this.OnServiceStateChange != null)
     {
         this.OnServiceStateChange(this, e);
     }
 }
Exemplo n.º 4
0
        /// <summary>
        /// ֹͣ����
        /// </summary>
        /// <param name="serviceName">������</param>
        /// <returns></returns>
        public bool StopService(string serviceName)
        {
            bool isOK = false;
            SmServiceEventArgs es = new SmServiceEventArgs();
            es.ServiceName = serviceName;
            es.ServiceOption = ServiceOptions.Stop;

            if (IsExisted(serviceName))
            {
                es.CompleteState = ServiceCompleteStateType.Trying;
                onServiceStateChange(es);

                if (CanStop(serviceName))
                {
                    System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
                    if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                    {
                        try
                        {
                            service.Stop();

                            for (int i = 0; i < 30; i++)
                            {
                                service.Refresh();
                                System.Threading.Thread.Sleep(1000);
                                if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
                                {
                                    es.CompleteState = ServiceCompleteStateType.Successfully;
                                    es.Error = null;
                                    isOK = true;
                                    break;
                                }
                                if (i == 30)
                                {
                                    es.Error = new Exception("�����ڿ���ʱ���ڲ�����ʱ");
                                    es.CompleteState = ServiceCompleteStateType.Timeout;
                                }
                            }
                        }
                        catch
                        {
                            es.Error = new Exception("����ֹͣʧ��");
                            es.CompleteState = ServiceCompleteStateType.UncanStop;
                        }
                    }
                    service.Close();
                }
                else
                {
                    es.Error = new Exception("�����ܿ���Ϊֹͣ");
                    es.CompleteState = ServiceCompleteStateType.UncanStop;
                }
            }
            else
            {
                es.Error = new Exception("ָ���ķ��񲻴���");
                es.CompleteState = ServiceCompleteStateType.NoExist;
            }
            onServiceStateChange(es);
            return isOK;
        }
Exemplo n.º 5
0
        /// <summary>
        /// ��ʼ����
        /// </summary>
        /// <param name="serviceName">������</param>
        /// <returns></returns>
        public bool StartService(string serviceName)
        {
            bool isOK = false;
            SmServiceEventArgs es = new SmServiceEventArgs();

            es.ServiceName = serviceName;
            es.ServiceOption = ServiceOptions.Start;
            if (IsExisted(serviceName))
            {
                //�������
                es.CompleteState = ServiceCompleteStateType.Trying;
                onServiceStateChange(es);

                if (GetRunType(serviceName) != ServiceStartType.Disabled)
                {

                    //�����ǽ��õ�
                    System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
                    if (service.Status != ServiceControllerStatus.Running && service.Status != ServiceControllerStatus.StartPending)
                    {
                        //�����������е�
                        try { service.Start(); }
                        catch (Exception ex)
                        {
                            es.Error = ex;
                            es.CompleteState = ServiceCompleteStateType.Error;
                            onServiceStateChange(es);
                            return false;
                        }

                        for (int i = 0; i < 30; i++)
                        {
                            service.Refresh();
                            System.Threading.Thread.Sleep(1000);
                            if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                            {
                                es.CompleteState = ServiceCompleteStateType.Successfully;
                                es.Error = null;
                                onServiceStateChange(es);
                                isOK = true;
                                break;
                            }
                            if (i == 30)
                            {
                                es.Error = new Exception("�����ڿ���ʱ���ڲ�����ʱ");
                                es.CompleteState = ServiceCompleteStateType.Timeout;
                                isOK = true;
                            }
                        }
                    }
                    else
                    {
                        es.CompleteState = ServiceCompleteStateType.Successfully;
                        es.Error = new Exception("�����Ѿ�����");
                        onServiceStateChange(es);
                    }
                    service.Close();
                }
                else
                {
                    //���ǽ��õ�
                    es.Error = new Exception("�����ڽ���״̬���ܲ���");
                    es.CompleteState = ServiceCompleteStateType.Disabled;
                    onServiceStateChange(es);
                }
            }
            else
            {
                //���񲻴���
                es.Error = new Exception("ָ���ķ��񲻴���");
                es.CompleteState = ServiceCompleteStateType.NoExist;
                onServiceStateChange(es);
            }
            return isOK;
        }
Exemplo n.º 6
0
        /// <summary>
        /// �������
        /// </summary>
        /// <param name="serviceName">������</param>
        /// <returns></returns>
        public bool ResetService(string serviceName)
        {
            bool isOK = false;
            SmServiceEventArgs es = new SmServiceEventArgs();

            if (IsExisted(serviceName))
            {
                es.ServiceName = serviceName;
                es.ServiceOption = ServiceOptions.Reset;
                es.CompleteState = ServiceCompleteStateType.Trying;
                onServiceStateChange(es);

                if (StopService(serviceName))
                    if (StartService(serviceName)) { isOK = true; }

                //es.ServiceOption = ServiceOptions.Reset;
                //es.Error = !isOK;
                //if (!isOK) { es.CompleteState = ServiceCompleteStateType.Successfully; }
                //else { es.CompleteState = ServiceCompleteStateType.Error; }
                //es.Error = !isOK;
                //onServiceStateChange(es);
            }
            else
            {
                es.ServiceName = serviceName;
                es.ServiceOption = ServiceOptions.Reset;
                es.CompleteState = ServiceCompleteStateType.NoExist;
                es.Error = new Exception("ָ���ķ��񲻴���");
                onServiceStateChange(es);
            }

            return isOK;
        }
Exemplo n.º 7
0
        /// <summary>
        /// 停止服务
        /// </summary>
        /// <param name="serviceName">服务名</param>
        /// <returns></returns>
        public bool StopService(string serviceName)
        {
            bool isOK             = false;
            SmServiceEventArgs es = new SmServiceEventArgs();

            es.ServiceName   = serviceName;
            es.ServiceOption = ServiceOptions.Stop;

            if (IsExisted(serviceName))
            {
                es.CompleteState = ServiceCompleteStateType.Trying;
                onServiceStateChange(es);

                if (CanStop(serviceName))
                {
                    System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
                    if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                    {
                        try
                        {
                            service.Stop();

                            for (int i = 0; i < 30; i++)
                            {
                                service.Refresh();
                                System.Threading.Thread.Sleep(1000);
                                if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
                                {
                                    es.CompleteState = ServiceCompleteStateType.Successfully;
                                    es.Error         = null;
                                    isOK             = true;
                                    break;
                                }
                                if (i == 30)
                                {
                                    es.Error         = new Exception("服务在控制时间内操作超时");
                                    es.CompleteState = ServiceCompleteStateType.Timeout;
                                }
                            }
                        }
                        catch
                        {
                            es.Error         = new Exception("服务停止失败");
                            es.CompleteState = ServiceCompleteStateType.UncanStop;
                        }
                    }
                    service.Close();
                }
                else
                {
                    es.Error         = new Exception("服务不能控制为停止");
                    es.CompleteState = ServiceCompleteStateType.UncanStop;
                }
            }
            else
            {
                es.Error         = new Exception("指定的服务不存在");
                es.CompleteState = ServiceCompleteStateType.NoExist;
            }
            onServiceStateChange(es);
            return(isOK);
        }
Exemplo n.º 8
0
        /// <summary>
        /// 开始服务
        /// </summary>
        /// <param name="serviceName">服务名</param>
        /// <returns></returns>
        public bool StartService(string serviceName)
        {
            bool isOK             = false;
            SmServiceEventArgs es = new SmServiceEventArgs();

            es.ServiceName   = serviceName;
            es.ServiceOption = ServiceOptions.Start;
            if (IsExisted(serviceName))
            {
                //服务存在
                es.CompleteState = ServiceCompleteStateType.Trying;
                onServiceStateChange(es);

                if (GetRunType(serviceName) != ServiceStartType.Disabled)
                {
                    //服务不是禁用的
                    System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
                    if (service.Status != ServiceControllerStatus.Running && service.Status != ServiceControllerStatus.StartPending)
                    {
                        //服务不是运行中的
                        try { service.Start(); }
                        catch (Exception ex)
                        {
                            es.Error         = ex;
                            es.CompleteState = ServiceCompleteStateType.Error;
                            onServiceStateChange(es);
                            return(false);
                        }

                        for (int i = 0; i < 30; i++)
                        {
                            service.Refresh();
                            System.Threading.Thread.Sleep(1000);
                            if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                            {
                                es.CompleteState = ServiceCompleteStateType.Successfully;
                                es.Error         = null;
                                onServiceStateChange(es);
                                isOK = true;
                                break;
                            }
                            if (i == 30)
                            {
                                es.Error         = new Exception("服务在控制时间内操作超时");
                                es.CompleteState = ServiceCompleteStateType.Timeout;
                                isOK             = true;
                            }
                        }
                    }
                    else
                    {
                        es.CompleteState = ServiceCompleteStateType.Successfully;
                        es.Error         = new Exception("服务已经运行");
                        onServiceStateChange(es);
                    }
                    service.Close();
                }
                else
                {
                    //服是禁用的
                    es.Error         = new Exception("服务在禁用状态不能操作");
                    es.CompleteState = ServiceCompleteStateType.Disabled;
                    onServiceStateChange(es);
                }
            }
            else
            {
                //服务不存在
                es.Error         = new Exception("指定的服务不存在");
                es.CompleteState = ServiceCompleteStateType.NoExist;
                onServiceStateChange(es);
            }
            return(isOK);
        }