This commit is contained in:
wxt
2026-01-26 15:41:42 +08:00
parent 8533734111
commit 041da6e16e
6 changed files with 825 additions and 445 deletions

View File

@@ -24,65 +24,205 @@ namespace 全自动水压检测仪
uiDataGridView1.AutoGenerateColumns = false;
}
private void SafeInvoke(Action action)
{
if (action == null) return;
// 检查窗体是否已释放/句柄是否创建
if (this.IsDisposed || !this.IsHandleCreated)
{
System.Diagnostics.Debug.WriteLine("ScanImport窗体已释放或句柄未创建跳过UI操作");
return;
}
try
{
// 如果已经在UI线程直接执行否则Invoke
if (this.InvokeRequired)
{
this.Invoke(action);
}
else
{
action();
}
}
catch (ObjectDisposedException ex)
{
System.Diagnostics.Debug.WriteLine($"ScanImport UI操作时控件已释放: {ex.Message}");
}
catch (InvalidOperationException ex)
{
System.Diagnostics.Debug.WriteLine($"ScanImport UI操作异常: {ex.Message}");
}
}
private void save_Click(object sender, EventArgs e)
{
float diffpressure = 0,exit_temperature = 0, dwelltime = 0, temperature = 0;
float.TryParse(uiTextBox4.Text, out diffpressure);
float.TryParse(uiTextBox9.Text, out exit_temperature);
float.TryParse(uiTextBox5.Text, out dwelltime);
float.TryParse(uiTextBox1.Text, out temperature);
_repository.InsertScanItems(new ScanData
SafeInvoke(() =>
{
barcode = uiTextBox2.Text,
diffpressure = diffpressure,
exit_temperature = exit_temperature,
dwelltime = dwelltime,
IsHighMode = temperature > 0,
temperature = temperature
float diffpressure = 0, exit_temperature = 0, dwelltime = 0, temperature = 0;
float.TryParse(uiTextBox4?.Text ?? "", out diffpressure);
float.TryParse(uiTextBox9?.Text ?? "", out exit_temperature);
float.TryParse(uiTextBox5?.Text ?? "", out dwelltime);
float.TryParse(uiTextBox1?.Text ?? "", out temperature);
string temperatureMode = "";
if (uiCheckBox1?.Checked == true)
{
temperatureMode = "常温模式";
}
else if (uiCheckBox2?.Checked == true)
{
temperatureMode = "高温模式";
}
try
{
_repository.InsertScanItems(new ScanData
{
barcode = uiTextBox2?.Text ?? "",
diffpressure = diffpressure,
exit_temperature = exit_temperature,
dwelltime = dwelltime,
IsHighMode = temperature > 0,
temperature = temperature,
TemperatureMode = temperatureMode
});
// 重新加载数据
LoadData();
// 可选:清空输入框
ClearInputFields();
}
catch (Exception ex)
{
MessageBox.Show($"保存失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
});
LoadData();
}
private void ClearInputFields()
{
if (uiTextBox1 != null) uiTextBox1.Text = "";
if (uiTextBox2 != null) uiTextBox2.Text = "";
if (uiTextBox4 != null) uiTextBox4.Text = "";
if (uiTextBox5 != null) uiTextBox5.Text = "";
if (uiTextBox9 != null) uiTextBox9.Text = "";
if (uiCheckBox1 != null) uiCheckBox1.Checked = false;
if (uiCheckBox2 != null) uiCheckBox2.Checked = false;
}
private void ScanImport_Load(object sender, EventArgs e)
{
LoadData();
SafeInvoke(() =>
{
LoadData();
});
}
private void LoadData()
{
var data = _repository.GetScanData();
uiDataGridView1.DataSource = data;
try
{
// 将数据查询操作移到后台线程避免UI卡顿
System.Threading.Tasks.Task.Run(() =>
{
var data = _repository.GetScanData();
uiDataGridView1.Refresh();
// 安全更新DataGridView
SafeInvoke(() =>
{
// 避免重复绑定导致的异常
if (uiDataGridView1 != null && !uiDataGridView1.IsDisposed)
{
// 先解绑再重新绑定
uiDataGridView1.DataSource = null;
uiDataGridView1.DataSource = data;
uiDataGridView1.Refresh();
}
});
});
}
catch (Exception ex)
{
SafeInvoke(() =>
{
MessageBox.Show($"加载数据失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
});
}
}
private void SwitchWindow<T>(ref T windowInstance, Func<T> createFunc) where T : UIForm
//private void SwitchWindow<T>(ref T windowInstance, Func<T> createFunc) where T : UIForm
//{
// if (windowInstance == null || windowInstance.IsDisposed)
// {
// windowInstance = createFunc();
// windowInstance.FormClosed += (s, e) =>
// {
// this.Invoke(new Action(() =>
// {
// this.Show();
// this.Activate();
// }));
// };
// }
// else
// {
// windowInstance.Activate();
// return;
// }
// this.Hide();
// windowInstance.Show();
//}
private void SwitchWindow<T>(T windowInstance, Func<T> createFunc) where T : UIForm
{
if (windowInstance == null || windowInstance.IsDisposed)
// 需要将 windowInstance 声明为局部变量
T localInstance = windowInstance;
// 复用窗口实例
if (localInstance == null || localInstance.IsDisposed)
{
windowInstance = createFunc();
windowInstance.FormClosed += (s, e) =>
localInstance = createFunc();
localInstance.FormClosed += (s, e) =>
{
this.Invoke(new Action(() =>
SafeInvoke(() =>
{
this.Show();
this.Activate();
}));
});
};
}
else
{
windowInstance.Activate();
SafeInvoke(() => localInstance.Activate());
return;
}
this.Hide();
windowInstance.Show();
}
SafeInvoke(() =>
{
this.Hide();
localInstance.Show();
});
// 如果需要更新外部引用,可以返回实例
// return localInstance;
}
private void uiButton1_Click(object sender, EventArgs e)
{
SwitchWindow(ref _normalTemperatureMode, () => new NormalTemperatureMode());
SwitchWindow(_normalTemperatureMode, () => new NormalTemperatureMode());
}
//清除
private void uiButton2_Click(object sender, EventArgs e)
{
}
}
}