首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我正在尝试使用OpenHardwareMonitor源代码,但从来没有得到我的视频卡温度,会有什么问题呢?

我正在尝试使用OpenHardwareMonitor源代码,但从来没有得到我的视频卡温度,会有什么问题呢?
EN

Stack Overflow用户
提问于 2012-08-01 05:30:39
回答 1查看 2.3K关注 0票数 2

在form1中,我做到了:

代码语言: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();
            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)
        {

        }
    }
}

它永远不会进入每一个跳过它的森林。我试了这么多示例,都搞不懂它是怎么工作的。我确信我的视频卡支持它,因为如果我运行的是原始的开放硬件监控程序ikts,它会显示所有的参数,比如温度和速度……

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

http://openhardwaremonitor.org/downloads/动态链接库位于程序本身的目录中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-01 05:50:19

我打开了ILSpy并查看了下载附带的示例exe,因为文档非常初级--通常是这样的:-)

我注意到在初始化Computer对象时,像GPUEnabled这样的布尔属性被设置为true。

所以..。

代码语言:javascript
复制
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窗体应用程序运行该代码时,我得到了当前温度(38摄氏度,这意味着我显然运行得不够努力!)

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

更新:

为了回答你在评论中提出的另一个问题,下面这样的例子应该适用于你:

代码语言:javascript
复制
    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事件就会触发一次,枚举硬件,并显示一个包含当前temp的消息框。这很容易成为一个标签,您甚至可以将Sensor存储在一个类级变量中,以避免每次都枚举IHardware集合。

希望这能有所帮助!

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

https://stackoverflow.com/questions/11749306

复制
相关文章

相似问题

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