This commit is contained in:
xyy
2026-01-09 16:44:01 +08:00
parent 70e3a35ec7
commit ecf820a251
2 changed files with 37 additions and 17 deletions

View File

@@ -320,22 +320,31 @@ namespace 外科辅料和患者防护罩激光抗性测试仪
{ {
uiLabel31.Text = currentRunStatus ? "运行中" : "空闲中"; uiLabel31.Text = currentRunStatus ? "运行中" : "空闲中";
})); }));
// ========== 关键修改1移除重复的插入逻辑只保留一段 ==========
// 关键修复:只在状态从运行变为停止时插入报表 // 仅在「运行→停止」且未上报过的情况下执行一次插入
if (_lastRunStatus && !currentRunStatus && !_isReported) if (_lastRunStatus && !currentRunStatus && !_isReported)
{ {
// 添加延迟确保数据稳定 // 添加延迟确保数据稳定
await Task.Delay(500); await Task.Delay(500);
// 加锁防止异步并发重复执行 // 加锁防止异步并发重复执行
await _asyncLock.WaitAsync(); await _asyncLock.WaitAsync();
try
var model = await InsertReportAsync();
if (model != null && !string.IsNullOrEmpty(model.Id2))
{ {
conductivityRepository.InsertReportItems(model); // 二次校验,防止锁等待期间状态已变更
_isReported = true; if (!_isReported)
//Debug.WriteLine($"报表插入成功:样品{model.sampleType}, 分类{model.result}"); {
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}"); Debug.WriteLine($"读取状态失败: {ex.Message}");
} }
finally { _asyncLock.Release(); } //finally { _asyncLock.Release(); }
} }
public async Task<bool[]> ReadModbusCoilAsync(ushort registerAddress, ushort readCount) public async Task<bool[]> ReadModbusCoilAsync(ushort registerAddress, ushort readCount)
{ {

View File

@@ -265,20 +265,31 @@ namespace 外科辅料和患者防护罩激光抗性测试仪
{ {
uiLabel23.Text = currentRunStatus ? "运行中" : "空闲中"; uiLabel23.Text = currentRunStatus ? "运行中" : "空闲中";
})); }));
// 关键修复:只在状态从运行变为停止时插入报表 // ========== 关键修改1移除重复的插入逻辑只保留一段 ==========
// 仅在「运行→停止」且未上报过的情况下执行一次插入
if (_lastRunStatus && !currentRunStatus && !_isReported) if (_lastRunStatus && !currentRunStatus && !_isReported)
{ {
// 添加延迟确保数据稳定 // 添加延迟确保数据稳定
await Task.Delay(500); await Task.Delay(500);
// 加锁防止异步并发重复执行 // 加锁防止异步并发重复执行
await _asyncLock.WaitAsync(); await _asyncLock.WaitAsync();
try
var model = await InsertReportAsync();
if (model != null && !string.IsNullOrEmpty(model.Id3))
{ {
conductivityRepository.InsertReportItems(model); // 二次校验,防止锁等待期间状态已变更
_isReported = true; if (!_isReported)
//Debug.WriteLine($"报表插入成功:样品{model.sampleType}, 分类{model.result}"); {
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(); // 释放锁
} }
} }