0

现在它显示温度一次。我希望它像在 openhardwaremonitor 原始程序中一样工作,温度每秒变化/更新一次,或者根据温度是否有任何变化,我不确定它是如何在那里工作的。

我所做的是从构造函数中取出所有 foreach 循环并将其放入计时器滴答事件中。但它不起作用。在 openhardwaremonitor 中,它的显示更改为 51c,而在我的应用程序中,它一直在 44c 上,就像我第一次运行应用程序时一样。

我在 textBox 行上使用了一个断点,有时它到达那里,有时它只是在执行内部循环,但 dosent 将 if 和 dosent 传递到消息框,如果它确实到达消息框,它仍然显示 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);
                        }
                    }
                }

            }
        }
    }
}
  1. 为什么每次执行循环时它都会更新它不会到达 textBox 行?

  2. 如果温度值发生变化或其与 openhardwaremonitor 原始程序中的计时器一起使用,它是否会更新温度?

4

2 回答 2

2

实际上,我在您之前的问题上发布了对此的部分答案,但将其包含在此处以供参考。

您的问题实际上主要是关于 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 评论 - 谢谢 Mike!

于 2012-07-31T22:34:55.407 回答
0

我不确定你为什么要在循环中这样做。我猜这会导致问题,因为如果循环在下一个滴答声之前还没有完成,他们可能会互相踩踏。

假设该 GPU 中只有 1 个 GPU 和 1 个温度传感器,为什么不这样做:

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

您将完全删除不必要的循环,并且应该使该方法快速运行。

于 2012-07-31T22:36:27.547 回答