Cgw.Common.ServiceRun.OnStop C# (CSharp) Method

OnStop() protected method

重载windows服务停止函数,调用服务启动类的Stop函数
protected OnStop ( ) : void
return void
        protected override void OnStop()
        {
            //在这里通知系统退出。注意,还是有30秒的要求。应该均匀分配各个退出场景需要的时间。如果30秒实在不够用,可以调用RequestAdditionalTime这个函数获得更长点的时间。不过不能太长或不能使用动态增长方式,否则最后会导致服务无法停止。
            if (this.SmcInit != null)
            {
                this.SmcInit.Stop();
            }
            //关闭服务、进程不退出,强制关掉进程
            Thread th = new Thread(new ThreadStart(() =>
            {
                {
                    ServiceController cs = new ServiceController();
                    cs.ServiceName = "HUAWEI SMC 2.0 ConvergeGateway";
                    cs.Refresh();

                    if (cs.Status == ServiceControllerStatus.Running || cs.Status == ServiceControllerStatus.StartPending)
                    {
                        Thread.Sleep(1);
                    }
                    else
                    {
                        Environment.Exit(0);
                    }

                }
            }));
            th.Start();
        }