gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:26:28 25 4
gpt4 key购买 nike

在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com