十年河东,十年河西,莫欺少年穷
学无止境,精益求精
主要作用 用于监控其他服务,如果其他服务挂了,则进行重启
代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; using System.Threading.Tasks; namespace ServiceDogStart { public partial class ServiceDog : ServiceBase { public ServiceDog() { InitializeComponent(); } System.Timers.Timer timer1; //计时器 System.Timers.Timer timer2; //计时器 System.Timers.Timer timer3; //计时器 int min = 30; //要设置成60的分之一 List<string> TestServerServices = new List<string>() { "BatteryService_Test", "BatteryStartService" }; protected override void OnStart(string[] args) { timer1 = new System.Timers.Timer(); timer1.Interval = 1000 * 60 * 5; //用于启动另外一个看门狗 timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed); timer1.Enabled = true; timer1.Start(); // timer2 = new System.Timers.Timer(); timer2.Interval = 1000 * 60 * min; //设置计时器事件间隔执行时间 每隔30分钟执行一次 timer2.Elapsed += new System.Timers.ElapsedEventHandler(timer2_Elapsed); timer2.Enabled = true; timer2.Start(); // timer3 = new System.Timers.Timer(); timer3.Interval = 1000 * 60 * 60; //设置计时器事件间隔执行时间 每隔120分钟执行一次 timer3.Elapsed += new System.Timers.ElapsedEventHandler(timer3_Elapsed); timer3.Enabled = true; timer3.Start(); } protected override void OnStop() { timer1.Enabled = false; timer2.Enabled = false; timer3.Enabled = false; } /// <summary> /// 启动另外一个看门狗 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { ServiceController service = new ServiceController("BatteryServiceStart"); try { if (service.Status != ServiceControllerStatus.Running) { service.Start(); service.WaitForStatus(ServiceControllerStatus.Running); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } /// <summary> /// 根据时间 循环启动电柜服务 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer2_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { var Number = 24 * 60 / min; for (int i = 0; i < Number; i++) { var d = DateTime.Now.Minute + DateTime.Now.Hour * 60; var minTim = i * min; var maxTim = i * min + min; if (d > minTim && d < maxTim) { Start(i % 5); } } } /// <summary> /// 启动电量统计 电池OTA 电池服务 预警中心 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer3_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { List<string> lst = new List<string>() { "BattertCabinetExecService", "BatteryOtaService", "BatteryService", "BatteryPoliceCenter" }; foreach(var item in lst) { ServiceController service = new ServiceController(item); try { if (service.Status != ServiceControllerStatus.Running) { service.Start(); service.WaitForStatus(ServiceControllerStatus.Running); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } } /// <summary> /// 循环重启电柜服务--每隔min==30分钟 /// </summary> /// <param name="Number"></param> private static void Start(int Number) { List<string> serviceLst = new List<string>(); switch (Number) { case 0: serviceLst = new List<string>() { "BatteryCabinetService", "BatteryCabinetService_1" }; break; case 1: serviceLst = new List<string>() { "BatteryCabinetService_2", "BatteryCabinetService_3" }; break; case 2: serviceLst = new List<string>() { "BatteryCabinetService_4", "BatteryCabinetService_5" }; break; case 3: serviceLst = new List<string>() { "BatteryCabinetService_6", "BatteryCabinetService_7" }; break; case 4: serviceLst = new List<string>() { "BatteryCabinetService_8", "BatteryCabinetService_9" }; break; } foreach(var item in serviceLst) { ServiceController service = new ServiceController(item); try { if (service.Status != ServiceControllerStatus.Running) { service.Start(); service.WaitForStatus(ServiceControllerStatus.Running); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } } } }