Monitor.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Windows.Forms;
  5. namespace FrmMonitor
  6. {
  7. public partial class Monitor : Form
  8. {
  9. public Monitor()
  10. {
  11. InitializeComponent();
  12. }
  13. private string oldFtpTruNo = "";//FTP图片上传用到,防止重复执行上传操作
  14. private PerformanceCounter p2 = null;
  15. private bool boolClose = false;//false检查终端程序是否存在,如果不存在,那么重新启动
  16. //ImageControl imageControl = new ImageControl(); //图片控制器含图片压缩及上传sftp
  17. private int count = 0;//终端CPU占用率超时时长
  18. private bool restartProcess = true; //在特定的时间段内重启程序
  19. private void Monitor_Load(object sender, EventArgs e)
  20. {
  21. this.WindowState = FormWindowState.Minimized;
  22. p2 = new PerformanceCounter("Process", "% Processor Time", "CarMeterSystem");
  23. //2021年3月7日将图片上传的线程挪动到此处;2021年3月8日不用挪了,还放回去
  24. //imageControl.Start();
  25. }
  26. private void timer1_Tick(object sender, EventArgs e)
  27. {
  28. try
  29. {
  30. //this.Hide();
  31. Process[] myprocess = Process.GetProcessesByName("CarMeterSystem");
  32. if (myprocess.Length <= 0)
  33. {//终端程序不存在,那么进行重启操作
  34. //intFlag = 1;
  35. Process.Start("CarMeterSystem.exe");
  36. WriteLog("终端进程(CarMeterSystem)已经重新启动");
  37. return;
  38. }
  39. try
  40. {
  41. decimal dcmCpu_ = Math.Round(Convert.ToDecimal(p2.NextValue() / Environment.ProcessorCount));//终端进程CPU占用率
  42. WriteLog("终端进程(CarMeterSystem)CPU占用率:" + dcmCpu_.ToString());
  43. if (dcmCpu_ > 20 && myprocess.Length > 0)
  44. {
  45. //2021年3月16日,杨秀东新增;5s一次,连续5次,若截图失败等原因造成采集卡死,则重启终端
  46. count++;
  47. if (count > 4)
  48. {
  49. count = 0;
  50. foreach (Process process in myprocess)
  51. {
  52. if (process.ProcessName == "CarMeterSystem")
  53. {
  54. //process.Kill();
  55. //WriteLog("终端进程(CarMeterSystem)已经被kill:" + dcmCpu_.ToString());
  56. //Process.Start("CarMeterSystem.exe");
  57. //WriteLog("终端进程(CarMeterSystem)已经重新启动");
  58. }
  59. }
  60. }
  61. }
  62. //2021年6月5日:每天晚上11点自动重启
  63. TimeSpan nowDt = DateTime.Now.TimeOfDay;
  64. TimeSpan workStartDT = DateTime.Parse("23:45").TimeOfDay;
  65. TimeSpan workEndDT = DateTime.Parse("23:50").TimeOfDay;
  66. if (nowDt > workStartDT && nowDt < workEndDT)
  67. {
  68. if (restartProcess)
  69. {
  70. foreach (Process process in myprocess)
  71. {
  72. if (process.ProcessName == "CarMeterSystem")
  73. {
  74. process.Kill();
  75. WriteLog("终端进程(CarMeterSystem)已经被kill:" + DateTime.Now.TimeOfDay);
  76. Process.Start("CarMeterSystem.exe");
  77. WriteLog("终端进程(CarMeterSystem)已经重新启动:" + DateTime.Now.TimeOfDay);
  78. }
  79. restartProcess = false;
  80. }
  81. }
  82. }
  83. else
  84. {
  85. restartProcess = true;
  86. }
  87. }
  88. catch (Exception exp)
  89. {
  90. WriteLog("CPU使用率监控异常!" + exp.Message);
  91. }
  92. }
  93. catch (Exception exp)
  94. {
  95. WriteLog("timer1_Tick异常:" + exp.Message);
  96. }
  97. }
  98. public void WriteLog(string str)
  99. {
  100. string m_szRunPath;
  101. m_szRunPath = System.Environment.CurrentDirectory;
  102. if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
  103. {
  104. System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
  105. }
  106. string strDate = System.DateTime.Now.ToString("yyyyMMdd");
  107. string strPathFile = m_szRunPath + "\\log\\" + strDate;
  108. if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
  109. {
  110. Directory.CreateDirectory(strPathFile);
  111. }
  112. System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\监控程序运行_" + strDate + ".log", true);
  113. tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  114. tw.WriteLine(str);
  115. tw.WriteLine("\r\n");
  116. tw.Close();
  117. }
  118. private void Monitor_Shown(object sender, EventArgs e)
  119. {
  120. //2021年12月24日 取消隐藏
  121. //this.Visible = false;
  122. try
  123. {
  124. //imageControl.Stop();
  125. }
  126. catch { }
  127. }
  128. }
  129. }