我想問題是太多的foreach循環。 但我需要他們得到sensor.Value爲什麼在timer2 tick事件中調用兩個函數時,應用程序運行速度太慢?
這是兩個功能:
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();
}
}
}
}
}
}
而在定時器Tick事件:
private void timer2_Tick(object sender, EventArgs e)
{
gpuView();
cpuView();
}
如果我不在tick事件中調用這個函數,程序運行平穩b即使即時即時只調用其中一個功能,每n秒停留一次。
由於即時更新cpu和gpu溫度,我想快速更新它們,timer2設置爲間隔100。 我不確定問題是功能中的foreach循環太多了,或者間隔爲100,所以它試圖從傳感器獲得值太快,所以硬件無法過快地處理信息。
也許有一種方法可以刪除一些foreach循環? 我不想將所有的Form1代碼上傳到這裏。但所有的snes和硬件都與OpenHardwareMonitorApplication的OpenHardwareMonitor.dll連接。
高興地幫助你 – 2012-08-04 10:36:30