Form1.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Windows.Forms;
  10. namespace EAPClock
  11. {
  12. public partial class Form1 : Form
  13. {
  14. //EAP小程序的名称,可能是下面两个中的某一个
  15. static string eapName1 = "MAutoUpdate.application";
  16. static string eapName2 = "EapForIdle.application";
  17. string eapPath1 = @"C:\Users\XiangQi\Desktop\" + eapName1; //Eap小程序的 桌面 路径
  18. string eapPath2 = @"C:\Users\XiangQi\Desktop\" + eapName2;
  19. string eapPath_D1 = @"D:\" + eapName1; //Eap小程序的 D盘 路径
  20. string eapPath_D2 = @"D:\" + eapName2;
  21. string eapPath_Auto1 = @"C:\Users\XiangQi\Desktop\123\" + eapName1; //Eap小程序的 自启动目录的 路径
  22. string eapPath_Auto2 = @"C:\Users\XiangQi\Desktop\123\" + eapName2;
  23. string eapProcessName = "EapForIdle";
  24. bool isStartOrNot; //判断EAP小程序是否启动
  25. int EAPClockRunCount = 0; //判本EAPClock此程序后台是否已运行。1表示没运行,大于1表示后台已运行此程序。
  26. //////下面是每个机型的机台路径:
  27. string WBDALHA_eapPath_desk1 = @"C:\Documents and Settings\Administrator\Desktop\" + eapName1;
  28. string WBDALHA_eapPath_desk2 = @"C:\Documents and Settings\Administrator\Desktop\" + eapName2;
  29. string WBDALHA_eapPath_D1 = @"D:\" + eapName1;
  30. string WBDALHA_eapPath_D2 = @"D:\" + eapName2;
  31. string WBDALHA_eapPath_auto1 = @"C:\Documents and Settings\Administrator\Start Menu\Programs\Startup\" + eapName1;
  32. string WBDALHA_eapPath_auto2 = @"C:\Documents and Settings\Administrator\Start Menu\Programs\Startup\" + eapName2;
  33. //WB、LHA、DA 机台的路径相同
  34. string AA_eapPath_desk1 = @"C:\Users\Administrator\Desktop\" + eapName1;
  35. string AA_eapPath_desk2 = @"C:\Users\Administrator\Desktop\" + eapName2;
  36. //AA的D盘路径和上面机台相同
  37. string AA_eapPath_auto1 = @"C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\" + eapName1;
  38. string AA_eapPath_auto2 = @"C:\Users\Administrator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\" + eapName1;
  39. public Form1()
  40. {
  41. InitializeComponent();
  42. //隐藏主窗体
  43. this.WindowState = FormWindowState.Minimized;
  44. //隐藏任务栏
  45. this.ShowInTaskbar = false;
  46. //解决 Alt+Tab可以调出主窗体
  47. SetVisibleCore(false);
  48. }
  49. ///// 禁用窗体的关闭按钮
  50. ///// 同时不让窗体出现在 Alt+Tab 中。 此步骤也不会出现在 进程 中,只有进程详情中才有
  51. private const int CP_NOCLOSE_BUTTON = 0x200;
  52. protected override CreateParams CreateParams
  53. {
  54. get
  55. {
  56. const int WS_EX_TOOLWINDOW = 0x80; // 不显示在Alt+Tab
  57. CreateParams myCp = base.CreateParams;
  58. myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
  59. myCp.ExStyle |= WS_EX_TOOLWINDOW; // 不显示在Alt+Tab
  60. return myCp;
  61. }
  62. }
  63. protected override void SetVisibleCore(bool value) //重写方法,目的是解决 Alt+Tab可以调出主窗体
  64. {
  65. base.SetVisibleCore(value);
  66. }
  67. private void Form1_Load(object sender, EventArgs e)
  68. {
  69. Control.CheckForIllegalCrossThreadCalls = false; //取消跨线程的检查
  70. MessageBox.Show("008版本");
  71. //AutoUpdate.CheckUpdate();
  72. Init();
  73. }
  74. /// <summary>
  75. /// 启动小程序的方法,让 AlwaysRun()方法死循环调用。根据小程序的不同路径来启动 (路径数组eapPathArry)
  76. /// </summary>
  77. public void StartMAutoUpdate()
  78. {
  79. string[] eapPathArryTest = { eapPath1, eapPath_D1, eapPath_Auto1, eapPath2, eapPath_D2, eapPath_Auto2 }; //EAP测试路径的 数组
  80. string[] eapPathArry = {
  81. WBDALHA_eapPath_desk1,
  82. WBDALHA_eapPath_D1,
  83. WBDALHA_eapPath_auto1,
  84. AA_eapPath_desk1,
  85. AA_eapPath_auto1,
  86. WBDALHA_eapPath_desk2,
  87. WBDALHA_eapPath_D2,
  88. WBDALHA_eapPath_auto2,
  89. AA_eapPath_desk2,
  90. AA_eapPath_auto2
  91. }; //EAP机台的 路径数组
  92. for (int i = 0; i < eapPathArryTest.Length; i++)
  93. {
  94. try
  95. {
  96. StartOnPath(eapPathArryTest[i]);
  97. }
  98. catch (Exception)
  99. {
  100. }
  101. if (isStartOrNot == true)
  102. {
  103. MessageBox.Show("监测到后台未开启EAP小程序,EAPClock已为您自动开启EAP");
  104. return;
  105. }
  106. }
  107. if (isStartOrNot == false)
  108. {
  109. MessageBox.Show("将小程序原文件放在下列任意一个目录中:\r\n\r\n 1-电脑桌面\r\n 2-D盘 \r\n 3-自启动目录", "小程序定时启动失败!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  110. }
  111. }
  112. /// <summary>
  113. /// 根据小程序的路径启动小程序
  114. /// </summary>
  115. /// <param name="path">传参小程序的路径</param>
  116. public void StartOnPath(string path)
  117. {
  118. isStartOrNot = false;
  119. ProcessStartInfo processStartInfo = new ProcessStartInfo(path);
  120. Process process = new Process();
  121. process.StartInfo = processStartInfo;
  122. isStartOrNot = process.Start();
  123. }
  124. /// <summary>
  125. /// 判断此程序EAPClock是否单例运行
  126. /// </summary>
  127. /// <returns>返回值runCount如果大于1,表示小程序已运行。如果等于1,表示只有当前程序在运行。</returns>
  128. public int IsOrNontOnlyOne()
  129. {
  130. //通过进程名获取当前进 此程序EAP 运行的实例,存放到进程数组中。
  131. Process[] processes = Process.GetProcessesByName("EAPClock");
  132. EAPClockRunCount = processes.Length;
  133. return EAPClockRunCount;
  134. }
  135. /// <summary>
  136. /// 初始化此程序。先判断后台是否已运行,如果没运行则调用 newThreadStart()
  137. /// </summary>
  138. public void Init()
  139. {
  140. IsOrNontOnlyOne();
  141. if (EAPClockRunCount > 1)
  142. {
  143. MessageBox.Show("后台已运行此定时系统");
  144. this.Close();
  145. Application.Exit();
  146. }
  147. else
  148. {
  149. newThreadStart();
  150. MessageBox.Show("已开启定时系统,欢迎使用");
  151. }
  152. }
  153. /// <summary>
  154. /// 开个新线程启动小程序.
  155. /// </summary>
  156. public void newThreadStart()
  157. {
  158. Thread thread = new Thread(AlwaysRun); //新线程调用AlwaysRun()死循环方法
  159. thread.IsBackground = true;
  160. thread.Start();
  161. }
  162. /// <summary>
  163. /// 循环检查是否运行小程序,如果后台没运行小程序,就启动小程序。
  164. /// </summary>
  165. public void AlwaysRun()
  166. {
  167. int runCount;
  168. while (true)
  169. {
  170. runCount = IsOrNotRun();
  171. if (runCount == 0)
  172. {
  173. StartMAutoUpdate();
  174. }
  175. // else MessageBox.Show("小程序已在后台开启,无需再次打开");
  176. Thread.Sleep(30000); //当前线程睡眠
  177. MessageBox.Show("008版本");
  178. }
  179. }
  180. /// <summary>
  181. /// 判断小程序是否在进程中运行,返回值runCount如果大于0,表示小程序已运行。如果等于0,表示未运行。
  182. /// </summary>
  183. public int IsOrNotRun()
  184. {
  185. //通过小程序进程名获取当前进小程序运行的实例,存放到进程数组中。
  186. Process[] processes = Process.GetProcessesByName(eapProcessName);
  187. int runCount = processes.Length;
  188. return runCount;
  189. }
  190. }
  191. }