首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用来自TextBox库的输出每n秒钟更新一次OpenHardwareMonitor

使用来自TextBox库的输出每n秒钟更新一次OpenHardwareMonitor
EN

Stack Overflow用户
提问于 2012-07-31 22:21:16
回答 2查看 2.6K关注 0票数 0

现在它只显示了一次温度。我希望它的工作像在开放硬件监控程序,每秒钟温度变化/更新,或根据温度有任何变化,我不知道它是如何工作在那里。

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

我在textBox行上使用了一个断点,有时它到达了那里,有时它只是做内部循环,但是剂量传递如果和剂量到达消息框,如果它到达消息箱,它仍然显示44c。

守则:

代码语言:javascript
复制
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监视器原始程序中的计时器一起工作?
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-31 22:34:55

实际上,我在你的earlier question上发布了对此的部分答复,但在这里包括它,以供参考。

您的问题实际上只是一个关于Windows.Forms.Timer的标准问题(或者您甚至可以使用BackgroundWorker或您选择的任何其他计时器来检查InvokeRequired)。

总之,下面是一个应该对您有用的简短示例:

代码语言:javascript
复制
    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注释--谢谢!

票数 2
EN

Stack Overflow用户

发布于 2012-07-31 22:36:27

我不知道你为什么要在循环中这样做。我猜想这会导致一个问题,因为如果循环在下一个滴答之前还没有完成,那么它们可能会互相踩在一起。

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

代码语言:javascript
复制
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);

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11749894

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档