예제 #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);
        }
예제 #2
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));
            }
        }