修复线程间操作无效错误
- 添加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)
|
private void btnDetect_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
Log("开始系统环境检测...");
|
SafeLog("开始系统环境检测...");
|
||||||
btnDetect.Enabled = false;
|
btnDetect.Enabled = false;
|
||||||
|
ClearAllInfo();
|
||||||
|
|
||||||
System.Threading.Tasks.Task.Run(() =>
|
System.Threading.Tasks.Task.Run(() =>
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// 清空现有信息
|
|
||||||
ClearAllInfo();
|
|
||||||
|
|
||||||
// 检测进度设置
|
// 检测进度设置
|
||||||
int totalSteps = 5;
|
int totalSteps = 5;
|
||||||
int currentStep = 0;
|
int currentStep = 0;
|
||||||
|
|
||||||
// 1. 检测系统信息
|
// 1. 检测系统信息
|
||||||
this.Invoke(new Action(() => UpdateProgress(++currentStep, totalSteps, "检测操作系统信息...")));
|
UpdateProgress(++currentStep, totalSteps, "检测操作系统信息...");
|
||||||
var systemInfo = GetSystemInfo();
|
var systemInfo = GetSystemInfo();
|
||||||
this.Invoke(new Action(() => txtSystemInfo.Text = systemInfo));
|
SafeUpdateTextBox(txtSystemInfo, systemInfo);
|
||||||
Log("系统信息检测完成");
|
SafeLog("系统信息检测完成");
|
||||||
|
|
||||||
// 2. 检测硬件信息
|
// 2. 检测硬件信息
|
||||||
this.Invoke(new Action(() => UpdateProgress(++currentStep, totalSteps, "检测硬件配置信息...")));
|
UpdateProgress(++currentStep, totalSteps, "检测硬件配置信息...");
|
||||||
var hardwareInfo = GetHardwareInfo();
|
var hardwareInfo = GetHardwareInfo();
|
||||||
this.Invoke(new Action(() => txtHardwareInfo.Text = hardwareInfo));
|
SafeUpdateTextBox(txtHardwareInfo, hardwareInfo);
|
||||||
Log("硬件信息检测完成");
|
SafeLog("硬件信息检测完成");
|
||||||
|
|
||||||
// 3. 检测网络信息
|
// 3. 检测网络信息
|
||||||
this.Invoke(new Action(() => UpdateProgress(++currentStep, totalSteps, "检测网络环境信息...")));
|
UpdateProgress(++currentStep, totalSteps, "检测网络环境信息...");
|
||||||
var networkInfo = GetNetworkInfo();
|
var networkInfo = GetNetworkInfo();
|
||||||
this.Invoke(new Action(() => txtNetworkInfo.Text = networkInfo));
|
SafeUpdateTextBox(txtNetworkInfo, networkInfo);
|
||||||
Log("网络信息检测完成");
|
SafeLog("网络信息检测完成");
|
||||||
|
|
||||||
// 4. 检测软件环境
|
// 4. 检测软件环境
|
||||||
this.Invoke(new Action(() => UpdateProgress(++currentStep, totalSteps, "检测软件环境信息...")));
|
UpdateProgress(++currentStep, totalSteps, "检测软件环境信息...");
|
||||||
var softwareInfo = GetSoftwareInfo();
|
var softwareInfo = GetSoftwareInfo();
|
||||||
this.Invoke(new Action(() => txtSoftwareInfo.Text = softwareInfo));
|
SafeUpdateTextBox(txtSoftwareInfo, softwareInfo);
|
||||||
Log("软件环境检测完成");
|
SafeLog("软件环境检测完成");
|
||||||
|
|
||||||
this.Invoke(new Action(() => UpdateProgress(++currentStep, totalSteps, "检测完成!")));
|
UpdateProgress(++currentStep, totalSteps, "检测完成!");
|
||||||
|
SafeLog("所有环境检测完成!");
|
||||||
this.Invoke(new Action(() => Log("所有环境检测完成!")));
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
this.Invoke(new Action(() => Log($"检测过程中发生错误: {ex.Message}")));
|
SafeLog($"检测过程中发生错误: {ex.Message}");
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
@ -132,25 +129,58 @@ namespace Environmental_testing
|
|||||||
|
|
||||||
private void ClearAllInfo()
|
private void ClearAllInfo()
|
||||||
{
|
{
|
||||||
txtSystemInfo.Clear();
|
SafeUpdateTextBox(txtSystemInfo, "");
|
||||||
txtHardwareInfo.Clear();
|
SafeUpdateTextBox(txtHardwareInfo, "");
|
||||||
txtNetworkInfo.Clear();
|
SafeUpdateTextBox(txtNetworkInfo, "");
|
||||||
txtSoftwareInfo.Clear();
|
SafeUpdateTextBox(txtSoftwareInfo, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void UpdateProgress(int current, int total, string status)
|
private void UpdateProgress(int current, int total, string status)
|
||||||
{
|
{
|
||||||
progressBar.Value = (int)((double)current / total * 100);
|
SafeUpdateProgressBar((int)((double)current / total * 100));
|
||||||
Log(status);
|
SafeLog(status);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Log(string msg)
|
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.AppendText($"[{DateTime.Now:HH:mm:ss}] {msg}\r\n");
|
||||||
txtLog.ScrollToCaret();
|
txtLog.ScrollToCaret();
|
||||||
Application.DoEvents();
|
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()
|
private string GetSystemInfo()
|
||||||
{
|
{
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user