现在它只显示了一次温度。我希望它的工作像在开放硬件监控程序,每秒钟温度变化/更新,或根据温度有任何变化,我不知道它是如何工作在那里。
我所做的就是从构造函数中取出所有的foreach循环,并将其放入一个timer蜱事件中。但这是有效的。在openhardwaremonitor监视器中,它的显示更改为51 c,而在我的应用程序中,它一直在44c上显示,就像我第一次运行应用程序时一样。
我在textBox行上使用了一个断点,有时它到达了那里,有时它只是做内部循环,但是剂量传递如果和剂量到达消息框,如果它到达消息箱,它仍然显示44c。
守则:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;
using OpenHardwareMonitor.Hardware;
namespace NvidiaTemp
{
public partial class Form1 : Form
{
Computer computer = new Computer();
public Form1()
{
InitializeComponent();
computer.Open();
computer.GPUEnabled = true;
timer1.Enabled = true;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
foreach (var hardwareItem in computer.Hardware)
{
if (hardwareItem.HardwareType == HardwareType.GpuNvidia)
{
foreach (var sensor in hardwareItem.Sensors)
{
if (sensor.SensorType == SensorType.Temperature)
{
// MessageBox.Show(String.Format("The current temperature is {0}", sensor.Value));
textBox1.Text = String.Format("The current temperature is {0}", sensor.Value);
}
}
}
}
}
}
}发布于 2012-07-31 22:34:55
实际上,我在你的earlier question上发布了对此的部分答复,但在这里包括它,以供参考。
您的问题实际上只是一个关于Windows.Forms.Timer的标准问题(或者您甚至可以使用BackgroundWorker或您选择的任何其他计时器来检查InvokeRequired)。
总之,下面是一个应该对您有用的简短示例:
Timer timer;
Computer myComputer;
ISensor GPUTemperatureSensor;
public Form1()
{
InitializeComponent();
myComputer = new Computer();
myComputer.Open();
myComputer.GPUEnabled = true;
foreach (var hardwareItem in myComputer.Hardware)
{
if (hardwareItem.HardwareType == HardwareType.GpuNvidia)
{
foreach (var sensor in hardwareItem.Sensors)
{
if (sensor.SensorType == SensorType.Temperature)
{
GPUTemperatureSensor = sensor;
}
}
}
}
timer = new Timer();
timer.Interval = 5000;
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
if(GPUTemperatureSensor != null)
{
GPUTemperatureSensor.Hardware.Update();//This line refreshes the sensor values
textBox1.Text = String.Format("The current temperature is {0}", GPUTemperatureSensor.Value);
}
else
{
textBox1.Text = "Could not find the GPU Temperature Sensor. Stopping.";
timer.Stop();
}
}实际上,我已经将传感器存储在类级别,以避免每次都枚举硬件集合。
我还为硬件上的Update()方法添加了@mikez注释--谢谢!
发布于 2012-07-31 22:36:27
我不知道你为什么要在循环中这样做。我猜想这会导致一个问题,因为如果循环在下一个滴答之前还没有完成,那么它们可能会互相踩在一起。
假设该GPU中只有一个GPU和一个温度传感器,为什么不这样做:
var hardwareItem = computer.Hardware.Single(h => h.HardwareType == HardwareType.GpuNvidia)
var sensor = hardwareItem.Sensors.Single(s => s.SensorType == SensorType.Temperature)
// MessageBox.Show(String.Format("The current temperature is {0}", sensor.Value));
textBox1.Text = String.Format("The current temperature is {0}", sensor.Value);您将完全删除不必要的循环,并且应该使该方法运行得更快。
https://stackoverflow.com/questions/11749894
复制相似问题