CPU temperature monitoring
Asked Answered
L

3

22

For a programming project I would like to access the temperature readings from my CPU and GPUs. I will be using C#. From various forums I get the impression that there is specific information and developer resources you need in order to access that information for various boards. I have a MSI NF750-G55 board. MSI's website does not have any of the information I am looking for. I tried their tech support and the rep I spoke with stated they do not have any such information. There must be a way to obtain that info.

Any thoughts?

Lozada answered 27/5, 2010 at 18:52 Comment(2)
There is a decent sample project at geekswithblogs.net/cicorias/archive/2006/11/22/97855.aspx that might help you get started. Direct link to the zip file containing the solution and all sources: cicoria.com/downloads/TemperatureMonitor/TempMonitorSrc.zipGermanous
Using Justin Niessner's solution lot of people got System.InvalidOperationException. In order to avoid it, the program should be run in administrative mode.Delastre
G
20

For at least the CPU side of things, you could use WMI.

The namespace\object is root\WMI, MSAcpi_ThermalZoneTemperature

Sample Code:

ManagementObjectSearcher searcher = 
    new ManagementObjectSearcher("root\\WMI",
                                 "SELECT * FROM MSAcpi_ThermalZoneTemperature");

ManagementObjectCollection collection = 
    searcher.Get();

foreach(ManagementBaseObject tempObject in collection)
{
    Console.WriteLine(tempObject["CurrentTemperature"].ToString());
}

That will give you the temperature in a raw format. You have to convert from there:

kelvin = raw / 10;

celsius = (raw / 10) - 273.15;

fahrenheit = ((raw / 10) - 273.15) * 9 / 5 + 32;
Grenadine answered 27/5, 2010 at 19:6 Comment(9)
I receive this error: 'enumerator.Current' threw an exception of type 'System.InvalidOperationException'Ree
@Ree - Interesting. I wonder if your motherboard manufacturer supports this WMI monitoring (not all support it).Grenadine
@Ree - I'm a newb and am working on implementing the code. So will see =].Lozada
I figured out how to implement the code however it I got an unhandled exception on while (enumerator.MoveNext())Lozada
@Lozada - For what it's worth, I did test the code before posting. It could be that your hardware doesn't support the monitoring.Grenadine
I got a ManagementException not supported on the enumerator.MoveNext() anyone else get this?Composition
Sadlyl, Most motherboards do not implement this via WMI. It throws an exception on my system as well.Digestible
This works sometimes and don't return neither CPU nor GPU temperature read this I used word sometimes because to get this value motherboard has to have at least one thermal sensor.Cassidycassie
I just want to point out that MSAcpi_ThermalZoneTemperature is NOT the CPU temp; but rather the temperature of an area of the motherboard... which is about as useful as measuring the temperature of a kitchen stove based on the temperature of the kitchen...Sherry
S
2

The best way to go for hardware related coding on windows is by using WMI which is a Code Creator tool from Microsoft, the tool will create the code for you based on what you are looking for in hardware related data and what .Net language you want to use.

The supported langauges currently are: C#, Visual Basic, VB Script.

Sellingplater answered 5/8, 2016 at 8:14 Comment(0)
C
0

Note that MSAcpi_ThermalZoneTemperature does not give you the temperature of the CPU but rather the temperature of the motherboard. Also, note that most motherboards do not implement this via WMI.

You can give the Open Hardware Monitor a go, although it lacks support for the latest processors.

internal sealed class CpuTemperatureReader : IDisposable
{
    private readonly Computer _computer;

    public CpuTemperatureReader()
    {
        _computer = new Computer { CPUEnabled = true };
        _computer.Open();
    }

    public IReadOnlyDictionary<string, float> GetTemperaturesInCelsius()
    {
        var coreAndTemperature = new Dictionary<string, float>();

        foreach (var hardware in _computer.Hardware)
        {
            hardware.Update(); //use hardware.Name to get CPU model
            foreach (var sensor in hardware.Sensors)
            {
                if (sensor.SensorType == SensorType.Temperature && sensor.Value.HasValue)
                    coreAndTemperature.Add(sensor.Name, sensor.Value.Value);
            }
        }

        return coreAndTemperature;
    }

    public void Dispose()
    {
        try
        {
            _computer.Close();
        }
        catch (Exception)
        {
            //ignore closing errors
        }
    }
}

Download the zip from the official source, extract and add a reference to OpenHardwareMonitorLib.dll in your project.

Cussed answered 8/8, 2018 at 7:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.