EAPClock.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Diagnostics;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Windows.Forms;
  8. namespace MAutoUpdate
  9. {
  10. public class EAPClock
  11. {
  12. //EAP小程序的名称,可能是下面两个中的某一个
  13. string eapName1 = AppDomain.CurrentDomain.BaseDirectory + "/EapForIdle.exe";
  14. string eapProcessName = "EapForIdle";
  15. string thisProgramProcessName = "MAutoUpdate";
  16. int thisProgramRunCount = 0; //判本此程序后台是否已运行。1表示没运行,大于1表示后台已运行此程序。
  17. int checkInterval = Convert.ToInt32(ConfigurationManager.AppSettings["RunCheckInterval"]) * 60;
  18. bool isStartOrNotEap; //判断EAP小程序是否启动
  19. /// <summary>
  20. /// 自动定时启动小程序。先判断后台 此程序 是否已运行,如果没运行则调用 newThreadStart()
  21. /// </summary>
  22. public void AutoStartEap()
  23. {
  24. StartEapForIdle();
  25. StartTimer();
  26. }
  27. public void StartTimer()
  28. {
  29. string errorinfo = string.Empty;
  30. System.Timers.Timer timer = new System.Timers.Timer();
  31. timer.Enabled = true;
  32. //设置执行一次(false)还是一直执行(true)
  33. timer.AutoReset = true;
  34. timer.Interval = checkInterval * 1000;
  35. timer.Elapsed += delegate
  36. {
  37. StartEapForIdle();
  38. };
  39. timer.Start();
  40. LogTool.AddLog("**********定时检查任务开启*************");
  41. }
  42. /// <summary>
  43. /// 判断此程序是否单例运行
  44. /// </summary>
  45. /// <returns>返回值thisProgramRunCount如果大于1,表示小程序已运行。如果等于1,表示只有当前程序在运行。</returns>
  46. public int IsOrNontOnlyOneThis()
  47. {
  48. //通过进程名获取当前进 此程序EAP 运行的实例,存放到进程数组中。
  49. Process[] processes = Process.GetProcessesByName(thisProgramProcessName);
  50. thisProgramRunCount = processes.Length;
  51. return thisProgramRunCount;
  52. }
  53. public bool IsRunning(string programName)
  54. {
  55. var processes = Process.GetProcessesByName(programName);
  56. if (processes == null || processes.Length == 0)
  57. return false;
  58. return true;
  59. }
  60. /// <summary>
  61. /// 开个新线程启动小程序.
  62. /// </summary>
  63. public void newThreadStart()
  64. {
  65. Thread thread = new Thread(AlwaysRun); //新线程调用AlwaysRun()死循环方法
  66. thread.IsBackground = true;
  67. thread.Start();
  68. }
  69. /// <summary>
  70. /// 循环检查是否已运行小程序,如果后台没运行小程序,就启动小程序。
  71. /// </summary>
  72. public void AlwaysRun()
  73. {
  74. int EAPRunCount;
  75. while (true)
  76. {
  77. EAPRunCount = IsOrNotRunEAP();
  78. if (EAPRunCount == 0)
  79. {
  80. StartEapForIdle();
  81. }
  82. // else MessageBox.Show("小程序已在后台开启,无需再次打开");
  83. Thread.Sleep(30000); //当前线程睡眠
  84. }
  85. }
  86. public void StartEapForIdle()
  87. {
  88. LogTool.AddLog("**********检查EAP小程序开启状态开始*************");
  89. LogTool.AddLog("小程序路径:" + eapName1);
  90. if (!IsRunning(eapProcessName))
  91. {
  92. try
  93. {
  94. Process.Start(eapName1);
  95. LogTool.AddLog("**********小程序被关闭,已重新开启*************");
  96. }
  97. catch (Exception ex)
  98. {
  99. LogTool.AddLog("自动重启EAP程序失败,原因为:" + ex.ToString());
  100. }
  101. }
  102. else
  103. {
  104. LogTool.AddLog("**********小程序运行中*************");
  105. }
  106. LogTool.AddLog("**********检查EAP小程序开启状态结束*************");
  107. }
  108. /// <summary>
  109. /// 判断小程序是否在进程中运行,返回值EAPRunCount如果大于0,表示小程序已运行。如果等于0,表示未运行。
  110. /// </summary>
  111. public int IsOrNotRunEAP()
  112. {
  113. //通过小程序进程名获取当前进小程序运行的实例,存放到进程数组中。
  114. Process[] processes = Process.GetProcessesByName(eapProcessName);
  115. int EAPRunCount = processes.Length;
  116. return EAPRunCount;
  117. }
  118. }
  119. }