修复线程间操作无效错误
- 添加SafeLog、SafeUpdateTextBox、SafeUpdateProgressBar方法处理跨线程UI更新 - 修复btnDetect_Click方法中的UI更新调用 - 确保所有后台线程对UI控件的访问都通过Invoke方法安全执行 - 修复"线程间操作无效: 从不是创建控件的线程访问它"错误 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
7934d00ffb
commit
a9a9d6a2c2
82
Form1.cs
82
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();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user