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();