gpt4 book ai didi

c# - 为什么在 timer2 tick 事件中调用两个函数时应用程序运行如此缓慢?

转载 作者:太空宇宙 更新时间:2023-11-03 16:28:11 24 4
gpt4 key购买 nike

我想问题是 foreach 循环太多了。但我需要他们来获取传感器。值

这是两个函数:

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


}

如果我不在 tick 事件中调用此函数,程序运行平稳,但即使我只调用其中一个函数,它每隔 n 秒就会卡住。

定时器 2 设置为间隔 100,因为我正在更新 cpu 和 gpu 温度,我想快速更新它们。我不确定问题是函数中的 foreach 循环太多,还是间隔为 100,所以它试图从传感器获取值的速度太快,以至于硬件无法太快地接收信息。

也许有一种方法可以删除一些 foreach 循环?我不想将我所有的 Form1 代码上传到这里。但是所有的传感器和硬件都与 OpenHardwareMonitorApplication 的 OpenHardwareMonitor.dll 连接。

最佳答案

你可以使用一个backgroundworker,它可以让你以简单的方式进行多线程。 它对您的情况很有用,并以异步方式调用 doWork 方法。在此方法中,循环调用您的两个函数。这里msdn说明http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx它比您设置随机间隔值的计时器更好,因为您必须让您的功能完成,这样如果必须发生事件,它就不会......

关于c# - 为什么在 timer2 tick 事件中调用两个函数时应用程序运行如此缓慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11805118/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com