using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO.Ports; using System.Threading; using Newtonsoft.Json.Linq; using Ini; using FileInfoClass; namespace DGPosPay.Window { public partial class WaitWindow : Form { FileClass fileclass = new FileClass(); Thread th; IniFile ini = new IniFile(); public string label3data = ""; public int countDownTime = 60; public string posData = ""; public string outstring = ""; private SerialPort serialPort = new SerialPort(); private System.Timers.Timer closeTimer; private bool isCountingDown = false; int closeDownTime = 60; public WaitWindow(SerialPort SerialPort) { InitializeComponent(); countDownTime = int.Parse(ini.IniReadValue("Config", "waitTime")); string closeDownTimeinfo = ini.IniReadValue("Config", "closeDownTime"); if (closeDownTimeinfo != "") { closeDownTime= int.Parse(closeDownTimeinfo); } this.label1.Text = "请操作POS,正在等待POS 返回....."; // 获取当前屏幕的工作区域 Rectangle screenRectangle = Screen.GetWorkingArea(this); // 计算窗体居中的位置 int left = screenRectangle.Left + (screenRectangle.Width - this.Width) / 2; int top = screenRectangle.Top + (screenRectangle.Height - this.Height) / 2; // 设置窗体的位置 this.Location = new Point(left, top); serialPort = SerialPort; if (th == null || !th.IsAlive) { th = new Thread(Run); th.IsBackground = true; th.Start(); } // 注册事件处理程序 this.FormClosing += MainForm_FormClosing; } // FormClosing 事件处理 - 允许取消关闭 private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { // e.CloseReason 包含关闭原因(用户点击按钮、程序调用等) if (e.CloseReason == CloseReason.UserClosing) { // 如果正在倒计时,忽略本次关闭请求 if (isCountingDown) { e.Cancel = true; return; } int warningTime = int.Parse(ini.IniReadValue("Config", "warningTime")); try { // 显示初始倒计时 UpdateCountdownLabel(closeDownTime); // 标记为正在倒计时 isCountingDown = true; // 防止多次启动计时器 if (closeTimer != null && closeTimer.Enabled) return; // 创建计时器 closeTimer = new System.Timers.Timer(1000); closeTimer.Elapsed += (s, args) => { if (closeDownTime > 0) { closeDownTime--; // 更新UI this.Invoke((MethodInvoker)delegate { UpdateCountdownLabel(closeDownTime); // 时间不足时改变颜色 if (closeDownTime <= warningTime) { label2.ForeColor = Color.Red; } }); } else { closeTimer.Stop(); // 移除事件处理,防止递归 this.FormClosing -= MainForm_FormClosing; // 关闭窗体 this.Invoke((MethodInvoker)delegate { this.Close(); }); } }; // 阻止默认关闭行为 e.Cancel = true; closeTimer.Start(); } catch (Exception ex) { MessageBox.Show($"关闭时出错: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); // 发生错误时允许正常关闭 e.Cancel = false; } } } private void UpdateCountdownLabel(int closeDownTime) { if ((closeDownTime!=0)&& (closeDownTime != 1)) { label2.Text = $"将在 {closeDownTime} 秒后关闭,请操作POS取消"; } } private void Run() { string errorCode = "-1"; string errorMessage = ""; JObject jObject = new JObject(); int warningTime = int.Parse(ini.IniReadValue("Config", "warningTime")); try { Thread.Sleep(1000); this.Invoke((MethodInvoker)delegate { this.TopMost = false; this.BringToFront(); this.TopMost = true; }); System.Timers.Timer timer = new System.Timers.Timer(1000); timer.Elapsed += (sender, e) => { if (countDownTime > 0) { countDownTime--; if (countDownTime < warningTime) { this.Invoke((MethodInvoker)delegate { this.label2.ForeColor = Color.Red; }); } if (posData == "") { if (serialPort.BytesToRead > 0) { StringBuilder txt = new StringBuilder(); while (serialPort.BytesToRead > 0) { string nextData = serialPort.ReadExisting(); txt.Append(nextData); Thread.Sleep(100); } string receivedData = txt.ToString(); string logPath = ini.IniReadValue("LOG", "path"); string DateTimeinfo = DateTime.Now.TimeOfDay.ToString(); fileclass.WriteLogFile(logPath, " POS返参: " + receivedData); posData = receivedData; countDownTime = warningTime; JObject posDataobj = JObject.Parse(posData); JObject jObject2 = new JObject(); string status = posDataobj["status"].ToString(); errorMessage = posDataobj["message"].ToString(); this.Invoke((MethodInvoker)delegate { this.label3.Text = errorMessage; }); if (status == "100") { errorCode = "0"; countDownTime = 0; closeDownTime = 0; } else { errorCode = status; } jObject2.Add("errorCode", errorCode); jObject2.Add("errorMessage", errorMessage); jObject2.Add("posData", receivedData); outstring = jObject2.ToString(); } } else { if (errorMessage != "") { this.Invoke((MethodInvoker)delegate { this.label3.Text = errorMessage; }); } this.Invoke((MethodInvoker)delegate { this.label2.Text = countDownTime + ""; }); closeDownTime = 0; } } else { timer.Stop(); this.Invoke((MethodInvoker)delegate { this.Close(); }); } }; timer.Start(); } catch (Exception ex2) { errorCode = "-1"; errorMessage = ex2.Message; JObject jObject2 = new JObject(); jObject2.Add("errorCode", errorCode); jObject2.Add("errorMessage", errorMessage); outstring = jObject2.ToString(); } } } }