WaitWindow.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. namespace DGPosPay.Window
  15. {
  16. public partial class WaitWindow : Form
  17. {
  18. Thread th;
  19. IniFile ini = new IniFile();
  20. public string label3data = "";
  21. public int countDownTime = 60;
  22. public string posData = "";
  23. public string outstring = "";
  24. private SerialPort serialPort = new SerialPort();
  25. public WaitWindow(SerialPort SerialPort)
  26. {
  27. InitializeComponent();
  28. countDownTime = int.Parse(ini.IniReadValue("Config", "waitTime"));
  29. this.label1.Text = "请操作POS,正在等待POS 返回.....";
  30. // 获取当前屏幕的工作区域
  31. Rectangle screenRectangle = Screen.GetWorkingArea(this);
  32. // 计算窗体居中的位置
  33. int left = screenRectangle.Left + (screenRectangle.Width - this.Width) / 2;
  34. int top = screenRectangle.Top + (screenRectangle.Height - this.Height) / 2;
  35. // 设置窗体的位置
  36. this.Location = new Point(left, top);
  37. serialPort = SerialPort;
  38. if (th == null || !th.IsAlive)
  39. {
  40. th = new Thread(Run);
  41. th.IsBackground = true;
  42. th.Start();
  43. }
  44. }
  45. private void Run()
  46. {
  47. string errorCode = "-1";
  48. string errorMessage = "";
  49. JObject jObject = new JObject();
  50. int warningTime = int.Parse(ini.IniReadValue("Config", "warningTime"));
  51. try
  52. {
  53. Thread.Sleep(1000);
  54. this.Invoke((MethodInvoker)delegate
  55. {
  56. this.TopMost = false;
  57. this.BringToFront();
  58. this.TopMost = true;
  59. });
  60. System.Timers.Timer timer = new System.Timers.Timer(1000);
  61. timer.Elapsed += (sender, e) =>
  62. {
  63. if (countDownTime > 0)
  64. {
  65. countDownTime--;
  66. if (countDownTime < warningTime)
  67. {
  68. this.Invoke((MethodInvoker)delegate
  69. {
  70. this.label2.ForeColor = Color.Red;
  71. });
  72. }
  73. if (posData == "")
  74. {
  75. if (serialPort.BytesToRead > 0)
  76. {
  77. string receivedData = serialPort.ReadExisting();
  78. posData = receivedData;
  79. countDownTime = warningTime;
  80. JObject posDataobj = JObject.Parse(posData);
  81. JObject jObject2 = new JObject();
  82. string status = posDataobj["status"].ToString();
  83. errorMessage = posDataobj["message"].ToString();
  84. this.Invoke((MethodInvoker)delegate
  85. {
  86. this.label3.Text = errorMessage;
  87. });
  88. if (status == "100")
  89. {
  90. errorCode = "0";
  91. countDownTime = 0;
  92. }
  93. else
  94. {
  95. errorCode = status;
  96. }
  97. jObject2.Add("errorCode", errorCode);
  98. jObject2.Add("errorMessage", errorMessage);
  99. jObject2.Add("posData", receivedData);
  100. outstring = jObject2.ToString();
  101. }
  102. }
  103. else
  104. {
  105. if (errorMessage != "")
  106. {
  107. this.Invoke((MethodInvoker)delegate
  108. {
  109. this.label3.Text = errorMessage;
  110. });
  111. }
  112. this.Invoke((MethodInvoker)delegate
  113. {
  114. this.label2.Text = countDownTime + "";
  115. });
  116. }
  117. }
  118. else
  119. {
  120. timer.Stop();
  121. this.Invoke((MethodInvoker)delegate
  122. {
  123. this.Close();
  124. });
  125. }
  126. };
  127. timer.Start();
  128. }
  129. catch (Exception ex2)
  130. {
  131. errorCode = "-1";
  132. errorMessage = ex2.Message;
  133. JObject jObject2 = new JObject();
  134. jObject2.Add("errorCode", errorCode);
  135. jObject2.Add("errorMessage", errorMessage);
  136. outstring = jObject2.ToString();
  137. }
  138. }
  139. }
  140. }