最近OpenHardwareMonitor示例代码C# [英] Recent OpenHardwareMonitor Sample Code C#

查看:251
本文介绍了最近OpenHardwareMonitor示例代码C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人知道在哪里可以找到任何近期的样本在C#中使用OpenHardwareMonitor.dll。
我已经尝试了简单的实现,但不能得到CPU的温度。我知道,因为应用程序本身得到的温度库必须拥有它。

Does anybody know where I can find any recent samples for using OpenHardwareMonitor.dll in C#. I've tried a simple implementation but cannot get the cpu Temp. I know the library must have it since the app itself gets the temperature.

我中有你必须要注册事件以获得对某些事物的读数感觉,但我发现很难找到一个很好的例子。

I have a feeling you have to register events to get readings on certain things but I'm finding it difficult to find a good example.

Computer myComputer = new Computer();

            myComputer.Open();

            foreach (var hardwareItem in myComputer.Hardware)
            {

                if (hardwareItem.HardwareType == HardwareType.CPU)
                {
                    foreach (var sensor in hardwareItem.Sensors)
                    {
                        if (sensor.SensorType == SensorType.Temperature)
                        {
                            Console.WriteLine(sensor.Value);
                        }
                    }
                }

            }

任何想法或洞察力,将不胜感激。

Any thoughts or insight would be appreciated.

推荐答案

下面是据我得到了。我没有更多的时间花在了,所以我就离开了侦探的其余部分给你。

Below is as far as I got. I don't have more time to spend on it, so I'll leave the rest of the sleuthing to you.

我找不到任何文档任何责任。有甚至没有在代码XML注释,所以我在源代码中挖出来获得尽可能我做到了。你将不得不这样做。

I couldn't find any documentation whatsoever. There are not even XML comments in the code, so I dug through the source code to get as far as I did. You're going to have to do the same.

您错过的第一件事是,你必须设置 Computer.CPUEnabled 属性设置为true你打电话之前 Computer.Open 。这将导致打开添加CPU的硬件设备。

The first thing you're missing is that you have to set the Computer.CPUEnabled property to true before you call Computer.Open. This causes Open to add a CPU hardware device.

您错过的第二件事是,你必须调用 Computer.Open

The second thing you're missing is that you have to call Computer.Open.

您错过的第三件事是,你必须调用 Hardware.Update 来。把它重新调查硬件

The third thing you're missing is that you have to call Hardware.Update to get it to re-poll the hardware.

你错过了第四件事是传感器的值是一个可空<浮动> 。你必须检查是否有一个有效的价值在那里。

The fourth thing you're missing is that the sensor value is a Nullable<float>. You have to check that there is a valid value there.

这仍然是不够的。虽然现在输出四个温度传感器(我的机器上),从未有一个温度值。

This still isn't enough. Although it now outputs four temperature sensors (on my machine), there never is a temperature value.

我通过设置挖,发现有对这些长期的初始化项目CPU和CPU的温度。我添加的代码把这些设置到电脑(为核心之一至少),但它并没有任何效果。

I dug through the settings and found that there are these long initialization items for the cpu and cpu's temperature. I added code to put those settings into the Computer (at least for one of the cores), but it didn't have any effect.

什么是失败的是此调用 IntelCpu.Update

What is failing is this call in IntelCpu.Update

Ring0.RdmsrTx(IA32_THERM_STATUS_MSR, out eax, out edx, 1UL << cpuid[i][0].Thread)

这就是为什么我怀疑有一些初始化丢失。

That's why I suspect there is some initialization missing.

祝您好运!

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using OpenHardwareMonitor.Hardware;

namespace WindowsFormsApplication1
{
    public class MySettings : ISettings
    {
        private IDictionary<string, string> settings = new Dictionary<string, string>();

        public MySettings(IDictionary<string, string> settings)
        {
            this.settings = settings;
        }

        public bool Contains(string name)
        {
            return settings.ContainsKey(name);
        }

        public string GetValue(string name, string value)
        {
            string result;
            if (settings.TryGetValue(name, out result))
                return result;
            else
                return value;
        }

        public void Remove(string name)
        {
            settings.Remove(name);
        }

        public void SetValue(string name, string value)
        {
            settings[name] = value;
        }
    }

    public class Form1 : Form
    {
        Computer myComputer;
        Timer timer = new Timer { Enabled = true, Interval = 1000 };

        public Form1()
        {
            timer.Tick += new EventHandler(timer_Tick);

            MySettings settings = new MySettings(new Dictionary<string, string>
            {
                { "/intelcpu/0/temperature/0/values", "H4sIAAAAAAAEAOy9B2AcSZYlJi9tynt/SvVK1+B0oQiAYBMk2JBAEOzBiM3mkuwdaUcjKasqgcplVmVdZhZAzO2dvPfee++999577733ujudTif33/8/XGZkAWz2zkrayZ4hgKrIHz9+fB8/Iu6//MH37x79i9/+NX6N3/TJm9/5f/01fw1+fosnv+A/+OlfS37/jZ/s/Lpv9fff6Ml/NTef/yZPnozc5679b+i193//TQZ+/w2Dd+P9/sZeX/67v/GTf/b3iP3u4/ObBL//73+i+f039+D8Zk/+xz/e/P6beu2TQZju8yH8f6OgzcvPv/U3/Rb8+z/0f/9b/+yfaOn8079X6fr6Cws7ln/iHzNwflPv99/wyS/+xY4+v/evcJ+733+jJ5//Cw7/4ndy9Im3+U2e/Fbnrk31C93vrt/fyPvdb+N//hsF7/4/AQAA//9NLZZ8WAIAAA==" },
                { "/intelcpu/0/load/0/values", "H4sIAAAAAAAEAOy9B2AcSZYlJi9tynt/SvVK1+B0oQiAYBMk2JBAEOzBiM3mkuwdaUcjKasqgcplVmVdZhZAzO2dvPfee++999577733ujudTif33/8/XGZkAWz2zkrayZ4hgKrIHz9+fB8/Iu6//MH37x79i9++mpwcv/md/9df89egZ/xX/ym/5y/4D37618Lv7ya//u+58+u+5d9/z7/5t/w9/6u5fP5bH/6av+eTkXyefXxp26ONaf/v/dG/sf39D/rvnv4e5vc/0IP56/waK/vuHzf5I38P8/tv+mv8Rbb9f0pwTF9/zr/1X9vP/8I//+/6Pf7Z30N+/zdf/HX29zd/859q4aCNP5b//U+U3/+7f+zXOjZwfqvDX/V7/o9/vPz+a1G/pv0f+fGlhfk7eZ//N3/0v28//5X0u/n8Cxq7+f1X/tHft20A5x8a/W5/02+BP36Nf+j/nv8XfzrT+c2//Ob4p3+vktvUhNs/+xcWikP6e/4T/5jS5M8/sL8vP/5ff49f/Ivl9//sHzv6PX/vXyG//9R/94/9HuZ34P/5vyC//3W/5e/1exa/k+Bw4bUBnU2bP4Xg/1bn0uafeTH6PatfKL//N3/0t2y/gG9+/8+IzqYNxmU+/+jwX7afY67/nwAAAP//GYSA31gCAAA=" },
            });

            myComputer = new Computer(settings) { CPUEnabled = true };
            myComputer.Open();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            Trace.WriteLine("");
            foreach (var hardwareItem in myComputer.Hardware)
            {
                if (hardwareItem.HardwareType == HardwareType.CPU)
                {
                    hardwareItem.Update();
                    foreach (IHardware subHardware in hardwareItem.SubHardware)
                        subHardware.Update();

                    foreach (var sensor in hardwareItem.Sensors)
                    {
                        if (sensor.SensorType == SensorType.Temperature)
                        {
                            Trace.WriteLine(String.Format("{0} Temperature = {1}", sensor.Name, sensor.Value.HasValue ? sensor.Value.Value.ToString() : "no value"));
                        }
                    }
                }
            }
        }
    }
}

这篇关于最近OpenHardwareMonitor示例代码C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆