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.
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.
The first thing you’re missing is that you have to set the
Computer.CPUEnabledproperty to true before you callComputer.Open. This causes Open to add a CPU hardware device.The second thing you’re missing is that you have to call
Computer.Open.The third thing you’re missing is that you have to call
Hardware.Updateto 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.
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.What is failing is this call in
IntelCpu.UpdateThat’s why I suspect there is some initialization missing.
Good Luck!