我想问题是有太多的 foreach 循环。但我需要他们来获取sensor.Value
这是两个函数:
private void cpuView()
{
Computer myComputer = new Computer();
myComputer = new Computer(settings) { CPUEnabled = true };
myComputer.Open();
Trace.WriteLine("");
foreach (var hardwareItem in myComputer.Hardware)
{
if (hardwareItem.HardwareType == HardwareType.CPU)
{
hardwareItem.Update();
foreach (IHardware subHardware in hardwareItem.SubHardware)
subHardware.Update();
foreach (var sensor in hardwareItem.Sensors)
{
settings.SetValue("sensor", sensor.Value.ToString());
if (sensor.SensorType == SensorType.Temperature)
{
sensor.Hardware.Update();
settings.GetValue("sensor", sensor.Value.ToString());
temperature_label.Text = sensor.Value.ToString() + "c";//String.Format("{0} Temperature = {1}c", sensor.Name, sensor.Value.HasValue ? sensor.Value.Value.ToString() : "no value");
}
}
}
}
}
第二个功能:
private void gpuView()
{
Computer computer = new Computer();
computer.Open();
computer.GPUEnabled = true;
foreach (var hardwareItem in computer.Hardware)
{
if (videoCardType("ati", "nvidia") == true)
{
HardwareType htype = HardwareType.GpuNvidia;
if (hardwareItem.HardwareType == htype)
{
foreach (var sensor in hardwareItem.Sensors)
{
if (sensor.SensorType == SensorType.Temperature)
{
sensor.Hardware.Update();
if (sensor.Value.ToString().Length > 0)
{
if (newGPULabel.Text.Length < 1)
{
if (UpdatingLabel(sensor.Value.ToString(), string.Empty))
{
label8.Text = newGPULabel.Text;
}
}
else if (UpdatingLabel(sensor.Value.ToString(), newGPULabel.Text.Substring(0, newGPULabel.Text.Length - 1)))
{
label8.Text = newGPULabel.Text;
}
newGPULabel.Text = sensor.Value.ToString() + "c";
label8.Visible = true;
}
int t = newGPULabel.Text.Length;
if (t >= 4)
{
newGPULabel.Location = new Point(210, 100);
}
else
{
newGPULabel.Location = new Point(250, 100);
}
timer2.Interval = 1000;
if (sensor.Value > 90)
{
Logger.Write("The current temperature is ===> " + sensor.Value);
button1.Enabled = true;
}
this.Select();
}
}
}
}
else
{
HardwareType htype = HardwareType.GpuAti;
if (hardwareItem.HardwareType == htype)
{
foreach (var sensor in hardwareItem.Sensors)
{
if (sensor.SensorType == SensorType.Temperature)
{
sensor.Hardware.Update();
if (sensor.Value.ToString().Length > 0)
{
if (newGPULabel.Text.Length < 1)
{
if (UpdatingLabel(sensor.Value.ToString(), string.Empty))
{
label8.Text = newGPULabel.Text;
}
}
else if (UpdatingLabel(sensor.Value.ToString(), newGPULabel.Text.Substring(0, newGPULabel.Text.Length - 1)))
{
label8.Text = newGPULabel.Text;
}
newGPULabel.Text = sensor.Value.ToString() + "c";
label8.Visible = true;
}
int t = newGPULabel.Text.Length;
if (t >= 4)
{
newGPULabel.Location = new Point(210, 100);
}
else
{
newGPULabel.Location = new Point(250, 100);
}
timer2.Interval = 1000;
if (sensor.Value > 90)
{
Logger.Write("The current temperature is ===> " + sensor.Value);
button1.Enabled = true;
}
this.Select();
}
}
}
}
}
}
在 timer2 滴答事件中:
private void timer2_Tick(object sender, EventArgs e)
{
gpuView();
cpuView();
}
如果我不在滴答事件中调用此函数,则程序运行平稳,但即使我只调用其中一个函数,它也会每 n 秒卡住一次。
timer2 设置为间隔 100,因为我更新了 cpu 和 gpu 温度,我想快速更新它们。我不确定问题是函数中的 foreach 循环过多还是间隔为 100,因此它试图从传感器中获取值的速度太快,因此硬件无法快速获取信息。
也许有一种方法可以删除一些 foreach 循环?我不想将我所有的 Form1 代码上传到这里。但是所有的snesor和硬件都连接到OpenHardwareMonitorApplication的OpenHardwareMonitor.dll。