Example #1
0
 /// <summary>
 /// 监听指令
 /// </summary>
 private static void MiniCommand()
 {
     try
     {
         var time = 2;
         int.TryParse(ConfigurationManager.AppSettings["ReadTime"], out time);
         while (!IsStoped)
         {
             try
             {
                 FileCopyWorker worker = new FileCopyWorker();
                 worker.Run();
             }
             catch (Exception e)
             {
                 LogerHelper.WriteErrorLog(e);
             }
             if (threadCommand.ThreadState == ThreadState.Running)
             {
                 Thread.Sleep(time * 1000);
             }
         }
     }
     catch (Exception e)
     {
         LogerHelper.WriteErrorLog(e);
     }
 }
Example #2
0
 /// <summary>
 /// 监听服务接收的指令信息
 /// </summary>
 public static void FileMonitoringCommand(FileSystemWatcher curWatcher)
 {
     //FileStream fs = new FileStream(@"E:\log.txt", FileMode.OpenOrCreate, FileAccess.Write);
     //StreamWriter sw = new StreamWriter(fs);
     //sw.BaseStream.Seek(0, SeekOrigin.End);
     //sw.WriteLine("WindowsService: Service Started" + DateTime.Now.ToString() + AppDomain.CurrentDomain.BaseDirectory + "\n");
     //sw.Flush();
     //sw.Close();
     //fs.Close();
     try
     {
         curWatcher = new FileSystemWatcher();
         curWatcher.BeginInit();
         curWatcher.Filter = "App.config";//只监视config文件的修改动作(例:fsw.Filter = "*.txt|*.doc|*.jpg")
         curWatcher.IncludeSubdirectories = false;
         curWatcher.Path = AppDomain.CurrentDomain.BaseDirectory.Replace("\\bin\\Debug", "");
         LogerHelper.WriteOperateLog("监控文件路径:" + curWatcher.Path);
         curWatcher.Changed            += new FileSystemEventHandler(OnFileChanged);
         curWatcher.EnableRaisingEvents = true;//开启监控
         curWatcher.EndInit();
     }
     catch (Exception ex)
     {
         LogerHelper.WriteErrorLog(ex);
     }
 }
Example #3
0
        /// <summary>
        /// 监听服务状态
        /// </summary>
        private static void MiniServiceStatus()
        {
            var time = 5;

            int.TryParse(ConfigurationManager.AppSettings["IntervalTime"], out time);
            string serviceName = ConfigurationManager.AppSettings["ServiceName"];

            while (true)
            {
                try
                {
                    ServiceController sc = new ServiceController(serviceName);
                    if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) ||
                        (sc.Status.Equals(ServiceControllerStatus.StopPending)))
                    {
                        sc.Start();
                        string msg = string.Format("{1}:服务{0}发生异常,开始重新启动!", serviceName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        LogerHelper.WriteOperateLog(msg);
                    }
                    sc.Refresh();
                }
                catch (Exception ex)
                {
                    LogerHelper.WriteErrorLog(ex);
                }
                if (thread.ThreadState == ThreadState.Running)
                {
                    Thread.Sleep(time * 1000);
                }
            }
        }
Example #4
0
 /// <summary>
 /// 启动某个服务
 /// </summary>
 /// <param name="serviceName"></param>
 private static void StartService(string serviceName)
 {
     try
     {
         ServiceController[] services = ServiceController.GetServices();
         foreach (ServiceController service in services)
         {
             if (service.ServiceName == serviceName)
             {
                 service.Start();
                 service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));//等待服务运行
             }
         }
         string msg = string.Format("{1}:服务{0}启动完成!", serviceName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
         LogerHelper.WriteOperateLog(msg);
     }
     catch (Exception ex)
     {
         LogerHelper.WriteErrorLog(ex);
     }
 }
Example #5
0
        /// <summary>
        /// 停止某个服务
        /// </summary>
        /// <param name="serviceName"></param>
        private static void StopService()
        {
            try
            {
                var serviceName = ConfigurationManager.AppSettings["ServiceName"];
                ServiceController[] services = ServiceController.GetServices();
                foreach (ServiceController service in services)
                {
                    if (service.ServiceName == serviceName)
                    {
                        service.Stop();
                        service.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 30));//等待服务停止
                    }
                }

                string msg = string.Format("{1}:服务{0}停止完成!", serviceName, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                LogerHelper.WriteOperateLog(msg);
            }
            catch (Exception ex)
            {
                LogerHelper.WriteErrorLog(ex);
            }
        }