c# - 我尝试使用 OpenHardwareMonitor 源代码,但从未获得我的显卡温度,这可能是什么问题?

标签 c#

在 form1 中我做了:

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();
            var temps = new List<decimal>();
            foreach (var hardware in computer.Hardware)
            {
                if (hardware.HardwareType != HardwareType.CPU)
                    continue;
                hardware.Update();
                foreach (var sensor in hardware.Sensors)
                {
                    if (sensor.SensorType != SensorType.Temperature)
                    {
                        if (sensor.Value != null)
                            temps.Add((decimal)sensor.Value);
                    }
                }
            }

            foreach (decimal temp in temps)
            {
                Console.WriteLine(temp);
                MessageBox.Show(temp.ToString());
            }
            Console.ReadLine();




        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

它永远不会进入 foreach 跳过它。 尝试了这么多示例无法弄清楚它是如何工作的。我确信我的显卡支持它,因为如果我运行 origiran openhardwaremonitor 程序 ikts 如何处理所有参数,如温度和速度...

刚刚从官方程序网站下载了openhwardwaremonitor.dll文件:

http://openhardwaremonitor.org/downloads/ 该dll位于程序本身的目录中。

最佳答案

我爆发了ILSpy并查看了下载附带的示例 exe,因为文档非常简陋 - 通常就是这种情况:-)

我注意到,当 Computer 对象初始化时,GPUEnabled 等 bool 属性被设置为 true。

所以...

Computer myComputer = new Computer();

myComputer.Open();

myComputer.GPUEnabled = true; //This is the line you are missing.

foreach (var hardwareItem in myComputer.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));
            }
        }
    }

}

当我从计算机上的 Windows 窗体应用程序运行该代码时,我得到当前温度(38C,这意味着我显然运行得不够努力!)

如果我不设置 GPUEnabled,我会得到与您完全相同的结果 - IHardware 集合中没有任何项目。

更新:

要回答您在评论中提出的其他问题,下面的示例应该适合您:

    Timer timer;

    Computer myComputer;

    public Form1()
    {

        InitializeComponent();



        myComputer = new Computer();

        myComputer.Open();

        myComputer.GPUEnabled = true;

        timer = new Timer();
        timer.Interval = 5000;
        timer.Tick += new EventHandler(timer_Tick);
        timer.Start();

     }

    void timer_Tick(object sender, EventArgs e)
    {
        foreach (var hardwareItem in myComputer.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));
                    }
                }
            }

        }
    }

这里我们有一个 Windows.Forms.TimerComputer 类作为类级别变量,我们在构造函数中初始化它们。每 5 秒,tick 事件就会触发,枚举硬件,并显示一个包含当前温度的消息框。这很容易成为一个标签,您甚至可以存储 Sensor 放在类级别变量中,以避免每次都枚举 IHardware 集合。

希望这有帮助!

关于c# - 我尝试使用 OpenHardwareMonitor 源代码,但从未获得我的显卡温度,这可能是什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11749306/

相关文章:

c# - XAML 使用单选按钮启用和禁用具有数据绑定(bind)的文本框

c# - 已知值属性的 XPath 查询

c# - 关于 MSVSMON.EXE 似乎没有运行的 Visual Studio 调试错误

c# - 如何在 C# 中编码 NDISUIO_QUERY_OID 结构以执行以下任务

c# - 底层连接已关闭错误

c# - asp.net 是否删除中继器中的 &lt;script&gt; 标记以及 mvc 中 <% for 循环 %> 内的 &lt;script&gt; 标记

c# - 如何删除 Entity Framework Core 中的多行?

c# - 上传文件到服务器抛出内存不足异常

c# - 将控制权传递给 ASP.NET 中的父类/基类的最佳方式

c# - 验证访问 token - Asp.Net 身份