From ecf820a251120126bfa2e891e9f3d04e41fac9bc Mon Sep 17 00:00:00 2001 From: xyy <544939200@qq.com> Date: Fri, 9 Jan 2026 16:44:01 +0800 Subject: [PATCH] --- .../外科辅料和患者防护罩激光抗性测试仪/PenetrationForm.cs | 29 ++++++++++++------- .../SecondaryIgnitionForm.cs | 25 +++++++++++----- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/外科辅料和患者防护罩激光抗性测试仪/外科辅料和患者防护罩激光抗性测试仪/PenetrationForm.cs b/外科辅料和患者防护罩激光抗性测试仪/外科辅料和患者防护罩激光抗性测试仪/PenetrationForm.cs index 4b6e716..f64d6eb 100644 --- a/外科辅料和患者防护罩激光抗性测试仪/外科辅料和患者防护罩激光抗性测试仪/PenetrationForm.cs +++ b/外科辅料和患者防护罩激光抗性测试仪/外科辅料和患者防护罩激光抗性测试仪/PenetrationForm.cs @@ -320,22 +320,31 @@ namespace 外科辅料和患者防护罩激光抗性测试仪 { uiLabel31.Text = currentRunStatus ? "运行中" : "空闲中"; })); - - // 关键修复:只在状态从运行变为停止时插入报表 + // ========== 关键修改1:移除重复的插入逻辑,只保留一段 ========== + // 仅在「运行→停止」且未上报过的情况下执行一次插入 if (_lastRunStatus && !currentRunStatus && !_isReported) { - // 添加延迟确保数据稳定 await Task.Delay(500); // 加锁防止异步并发重复执行 await _asyncLock.WaitAsync(); - - var model = await InsertReportAsync(); - if (model != null && !string.IsNullOrEmpty(model.Id2)) + try { - conductivityRepository.InsertReportItems(model); - _isReported = true; - //Debug.WriteLine($"报表插入成功:样品{model.sampleType}, 分类{model.result}"); + // 二次校验,防止锁等待期间状态已变更 + if (!_isReported) + { + var model = await InsertReportAsync(); + if (model != null && !string.IsNullOrEmpty(model.Id2)) + { + conductivityRepository.InsertReportItems(model); + _isReported = true; // 标记已上报 + Debug.WriteLine($"报表插入成功:样品{model.sampleType2}, 分类{model.result2}"); + } + } + } + finally + { + _asyncLock.Release(); // 释放锁 } } @@ -346,7 +355,7 @@ namespace 外科辅料和患者防护罩激光抗性测试仪 { Debug.WriteLine($"读取状态失败: {ex.Message}"); } - finally { _asyncLock.Release(); } + //finally { _asyncLock.Release(); } } public async Task ReadModbusCoilAsync(ushort registerAddress, ushort readCount) { diff --git a/外科辅料和患者防护罩激光抗性测试仪/外科辅料和患者防护罩激光抗性测试仪/SecondaryIgnitionForm.cs b/外科辅料和患者防护罩激光抗性测试仪/外科辅料和患者防护罩激光抗性测试仪/SecondaryIgnitionForm.cs index 17ea3ae..3cda07f 100644 --- a/外科辅料和患者防护罩激光抗性测试仪/外科辅料和患者防护罩激光抗性测试仪/SecondaryIgnitionForm.cs +++ b/外科辅料和患者防护罩激光抗性测试仪/外科辅料和患者防护罩激光抗性测试仪/SecondaryIgnitionForm.cs @@ -265,20 +265,31 @@ namespace 外科辅料和患者防护罩激光抗性测试仪 { uiLabel23.Text = currentRunStatus ? "运行中" : "空闲中"; })); - // 关键修复:只在状态从运行变为停止时插入报表 + // ========== 关键修改1:移除重复的插入逻辑,只保留一段 ========== + // 仅在「运行→停止」且未上报过的情况下执行一次插入 if (_lastRunStatus && !currentRunStatus && !_isReported) { // 添加延迟确保数据稳定 await Task.Delay(500); // 加锁防止异步并发重复执行 await _asyncLock.WaitAsync(); - - var model = await InsertReportAsync(); - if (model != null && !string.IsNullOrEmpty(model.Id3)) + try { - conductivityRepository.InsertReportItems(model); - _isReported = true; - //Debug.WriteLine($"报表插入成功:样品{model.sampleType}, 分类{model.result}"); + // 二次校验,防止锁等待期间状态已变更 + if (!_isReported) + { + var model = await InsertReportAsync(); + if (model != null && !string.IsNullOrEmpty(model.Id3)) + { + conductivityRepository.InsertReportItems(model); + _isReported = true; // 标记已上报 + Debug.WriteLine($"报表插入成功:样品{model.sampleType3}, 分类{model.result3}"); + } + } + } + finally + { + _asyncLock.Release(); // 释放锁 } }