WaitWindow.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO.Ports;
  11. using System.Threading;
  12. using Newtonsoft.Json.Linq;
  13. using Ini;
  14. using FileInfoClass;
  15. namespace DGPosPay.Window
  16. {
  17. public partial class WaitWindow : Form
  18. {
  19. FileClass fileclass = new FileClass();
  20. Thread th;
  21. IniFile ini = new IniFile();
  22. public string label3data = "";
  23. public int countDownTime = 60;
  24. public string posData = "";
  25. public string outstring = "";
  26. private SerialPort serialPort = new SerialPort();
  27. private System.Timers.Timer closeTimer;
  28. private bool isCountingDown = false;
  29. int closeDownTime = 60;
  30. public WaitWindow(SerialPort SerialPort)
  31. {
  32. InitializeComponent();
  33. countDownTime = int.Parse(ini.IniReadValue("Config", "waitTime"));
  34. string closeDownTimeinfo = ini.IniReadValue("Config", "closeDownTime");
  35. if (closeDownTimeinfo != "")
  36. {
  37. closeDownTime= int.Parse(closeDownTimeinfo);
  38. }
  39. this.label1.Text = "请操作POS,正在等待POS 返回.....";
  40. // 获取当前屏幕的工作区域
  41. Rectangle screenRectangle = Screen.GetWorkingArea(this);
  42. // 计算窗体居中的位置
  43. int left = screenRectangle.Left + (screenRectangle.Width - this.Width) / 2;
  44. int top = screenRectangle.Top + (screenRectangle.Height - this.Height) / 2;
  45. // 设置窗体的位置
  46. this.Location = new Point(left, top);
  47. serialPort = SerialPort;
  48. if (th == null || !th.IsAlive)
  49. {
  50. th = new Thread(Run);
  51. th.IsBackground = true;
  52. th.Start();
  53. }
  54. // 注册事件处理程序
  55. this.FormClosing += MainForm_FormClosing;
  56. }
  57. // FormClosing 事件处理 - 允许取消关闭
  58. private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
  59. {
  60. // e.CloseReason 包含关闭原因(用户点击按钮、程序调用等)
  61. if (e.CloseReason == CloseReason.UserClosing)
  62. {
  63. // 如果正在倒计时,忽略本次关闭请求
  64. if (isCountingDown)
  65. {
  66. e.Cancel = true;
  67. return;
  68. }
  69. int warningTime = int.Parse(ini.IniReadValue("Config", "warningTime"));
  70. try
  71. {
  72. // 显示初始倒计时
  73. UpdateCountdownLabel(closeDownTime);
  74. // 标记为正在倒计时
  75. isCountingDown = true;
  76. // 防止多次启动计时器
  77. if (closeTimer != null && closeTimer.Enabled)
  78. return;
  79. // 创建计时器
  80. closeTimer = new System.Timers.Timer(1000);
  81. closeTimer.Elapsed += (s, args) =>
  82. {
  83. if (closeDownTime > 0)
  84. {
  85. closeDownTime--;
  86. // 更新UI
  87. this.Invoke((MethodInvoker)delegate
  88. {
  89. UpdateCountdownLabel(closeDownTime);
  90. // 时间不足时改变颜色
  91. if (closeDownTime <= warningTime)
  92. {
  93. label2.ForeColor = Color.Red;
  94. }
  95. });
  96. }
  97. else
  98. {
  99. closeTimer.Stop();
  100. // 移除事件处理,防止递归
  101. this.FormClosing -= MainForm_FormClosing;
  102. // 关闭窗体
  103. this.Invoke((MethodInvoker)delegate
  104. {
  105. this.Close();
  106. });
  107. }
  108. };
  109. // 阻止默认关闭行为
  110. e.Cancel = true;
  111. closeTimer.Start();
  112. }
  113. catch (Exception ex)
  114. {
  115. MessageBox.Show($"关闭时出错: {ex.Message}", "错误",
  116. MessageBoxButtons.OK, MessageBoxIcon.Error);
  117. // 发生错误时允许正常关闭
  118. e.Cancel = false;
  119. }
  120. }
  121. }
  122. private void UpdateCountdownLabel(int closeDownTime)
  123. {
  124. if ((closeDownTime!=0)&& (closeDownTime != 1))
  125. {
  126. label2.Text = $"将在 {closeDownTime} 秒后关闭,请操作POS取消";
  127. }
  128. }
  129. private void Run()
  130. {
  131. string errorCode = "-1";
  132. string errorMessage = "";
  133. JObject jObject = new JObject();
  134. int warningTime = int.Parse(ini.IniReadValue("Config", "warningTime"));
  135. try
  136. {
  137. Thread.Sleep(1000);
  138. this.Invoke((MethodInvoker)delegate
  139. {
  140. this.TopMost = false;
  141. this.BringToFront();
  142. this.TopMost = true;
  143. });
  144. System.Timers.Timer timer = new System.Timers.Timer(1000);
  145. timer.Elapsed += (sender, e) =>
  146. {
  147. if (countDownTime > 0)
  148. {
  149. countDownTime--;
  150. if (countDownTime < warningTime)
  151. {
  152. this.Invoke((MethodInvoker)delegate
  153. {
  154. this.label2.ForeColor = Color.Red;
  155. });
  156. }
  157. if (posData == "")
  158. {
  159. if (serialPort.BytesToRead > 0)
  160. {
  161. StringBuilder txt = new StringBuilder();
  162. while (serialPort.BytesToRead > 0)
  163. {
  164. string nextData = serialPort.ReadExisting();
  165. txt.Append(nextData);
  166. Thread.Sleep(100);
  167. }
  168. string receivedData = txt.ToString();
  169. string logPath = ini.IniReadValue("LOG", "path");
  170. string DateTimeinfo = DateTime.Now.TimeOfDay.ToString();
  171. fileclass.WriteLogFile(logPath, " POS返参: " + receivedData);
  172. posData = receivedData;
  173. countDownTime = warningTime;
  174. JObject posDataobj = JObject.Parse(posData);
  175. JObject jObject2 = new JObject();
  176. string status = posDataobj["status"].ToString();
  177. errorMessage = posDataobj["message"].ToString();
  178. this.Invoke((MethodInvoker)delegate
  179. {
  180. this.label3.Text = errorMessage;
  181. });
  182. if (status == "100")
  183. {
  184. errorCode = "0";
  185. countDownTime = 0;
  186. closeDownTime = 0;
  187. }
  188. else
  189. {
  190. errorCode = status;
  191. }
  192. jObject2.Add("errorCode", errorCode);
  193. jObject2.Add("errorMessage", errorMessage);
  194. jObject2.Add("posData", receivedData);
  195. outstring = jObject2.ToString();
  196. }
  197. }
  198. else
  199. {
  200. if (errorMessage != "")
  201. {
  202. this.Invoke((MethodInvoker)delegate
  203. {
  204. this.label3.Text = errorMessage;
  205. });
  206. }
  207. this.Invoke((MethodInvoker)delegate
  208. {
  209. this.label2.Text = countDownTime + "";
  210. });
  211. closeDownTime = 0;
  212. }
  213. }
  214. else
  215. {
  216. timer.Stop();
  217. this.Invoke((MethodInvoker)delegate
  218. {
  219. this.Close();
  220. });
  221. }
  222. };
  223. timer.Start();
  224. }
  225. catch (Exception ex2)
  226. {
  227. errorCode = "-1";
  228. errorMessage = ex2.Message;
  229. JObject jObject2 = new JObject();
  230. jObject2.Add("errorCode", errorCode);
  231. jObject2.Add("errorMessage", errorMessage);
  232. outstring = jObject2.ToString();
  233. }
  234. }
  235. }
  236. }