From a9a9d6a2c2dd196e08ba329f10134a0c24f20150 Mon Sep 17 00:00:00 2001 From: Claude Assistant Date: Thu, 20 Nov 2025 15:46:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=BA=BF=E7=A8=8B=E9=97=B4?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E6=97=A0=E6=95=88=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加SafeLog、SafeUpdateTextBox、SafeUpdateProgressBar方法处理跨线程UI更新 - 修复btnDetect_Click方法中的UI更新调用 - 确保所有后台线程对UI控件的访问都通过Invoke方法安全执行 - 修复"线程间操作无效: 从不是创建控件的线程访问它"错误 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- Form1.cs | 82 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 56 insertions(+), 26 deletions(-) diff --git a/Form1.cs b/Form1.cs index b95dce1..aef4234 100644 --- a/Form1.cs +++ b/Form1.cs @@ -35,51 +35,48 @@ namespace Environmental_testing private void btnDetect_Click(object sender, EventArgs e) { - Log("开始系统环境检测..."); + SafeLog("开始系统环境检测..."); btnDetect.Enabled = false; + ClearAllInfo(); System.Threading.Tasks.Task.Run(() => { try { - // 清空现有信息 - ClearAllInfo(); - // 检测进度设置 int totalSteps = 5; int currentStep = 0; // 1. 检测系统信息 - this.Invoke(new Action(() => UpdateProgress(++currentStep, totalSteps, "检测操作系统信息..."))); + UpdateProgress(++currentStep, totalSteps, "检测操作系统信息..."); var systemInfo = GetSystemInfo(); - this.Invoke(new Action(() => txtSystemInfo.Text = systemInfo)); - Log("系统信息检测完成"); + SafeUpdateTextBox(txtSystemInfo, systemInfo); + SafeLog("系统信息检测完成"); // 2. 检测硬件信息 - this.Invoke(new Action(() => UpdateProgress(++currentStep, totalSteps, "检测硬件配置信息..."))); + UpdateProgress(++currentStep, totalSteps, "检测硬件配置信息..."); var hardwareInfo = GetHardwareInfo(); - this.Invoke(new Action(() => txtHardwareInfo.Text = hardwareInfo)); - Log("硬件信息检测完成"); + SafeUpdateTextBox(txtHardwareInfo, hardwareInfo); + SafeLog("硬件信息检测完成"); // 3. 检测网络信息 - this.Invoke(new Action(() => UpdateProgress(++currentStep, totalSteps, "检测网络环境信息..."))); + UpdateProgress(++currentStep, totalSteps, "检测网络环境信息..."); var networkInfo = GetNetworkInfo(); - this.Invoke(new Action(() => txtNetworkInfo.Text = networkInfo)); - Log("网络信息检测完成"); + SafeUpdateTextBox(txtNetworkInfo, networkInfo); + SafeLog("网络信息检测完成"); // 4. 检测软件环境 - this.Invoke(new Action(() => UpdateProgress(++currentStep, totalSteps, "检测软件环境信息..."))); + UpdateProgress(++currentStep, totalSteps, "检测软件环境信息..."); var softwareInfo = GetSoftwareInfo(); - this.Invoke(new Action(() => txtSoftwareInfo.Text = softwareInfo)); - Log("软件环境检测完成"); + SafeUpdateTextBox(txtSoftwareInfo, softwareInfo); + SafeLog("软件环境检测完成"); - this.Invoke(new Action(() => UpdateProgress(++currentStep, totalSteps, "检测完成!"))); - - this.Invoke(new Action(() => Log("所有环境检测完成!"))); + UpdateProgress(++currentStep, totalSteps, "检测完成!"); + SafeLog("所有环境检测完成!"); } catch (Exception ex) { - this.Invoke(new Action(() => Log($"检测过程中发生错误: {ex.Message}"))); + SafeLog($"检测过程中发生错误: {ex.Message}"); } finally { @@ -132,25 +129,58 @@ namespace Environmental_testing private void ClearAllInfo() { - txtSystemInfo.Clear(); - txtHardwareInfo.Clear(); - txtNetworkInfo.Clear(); - txtSoftwareInfo.Clear(); + SafeUpdateTextBox(txtSystemInfo, ""); + SafeUpdateTextBox(txtHardwareInfo, ""); + SafeUpdateTextBox(txtNetworkInfo, ""); + SafeUpdateTextBox(txtSoftwareInfo, ""); } private void UpdateProgress(int current, int total, string status) { - progressBar.Value = (int)((double)current / total * 100); - Log(status); + SafeUpdateProgressBar((int)((double)current / total * 100)); + SafeLog(status); } private void Log(string msg) { + SafeLog(msg); + } + + private void SafeLog(string msg) + { + if (txtLog.InvokeRequired) + { + txtLog.Invoke(new Action(() => SafeLog(msg))); + return; + } + txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] {msg}\r\n"); txtLog.ScrollToCaret(); Application.DoEvents(); } + private void SafeUpdateTextBox(TextBox textBox, string text) + { + if (textBox.InvokeRequired) + { + textBox.Invoke(new Action(() => SafeUpdateTextBox(textBox, text))); + return; + } + + textBox.Text = text; + } + + private void SafeUpdateProgressBar(int value) + { + if (progressBar.InvokeRequired) + { + progressBar.Invoke(new Action(() => SafeUpdateProgressBar(value))); + return; + } + + progressBar.Value = value; + } + private string GetSystemInfo() { var sb = new StringBuilder();