6

i want to fetch CPU Performance data in real time including temperature. i used the following code to get CPU Temperature:

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

            foreach (ManagementObject queryObj in searcher.Get())
            {
                double temp = Convert.ToDouble(queryObj["CurrentTemperature"].ToString());
                double temp_critical = Convert.ToDouble(queryObj["CriticalTripPoint"].ToString());
                double temp_cel = (temp/10 - 273.15);
                double temp_critical_cel = temp_critical / 10 - 273.15;
                lblCurrentTemp.Text = temp_cel.ToString();
                lblCriticalTemp.Text = temp_critical_cel.ToString();
            }
        }
        catch (ManagementException e)
        {
            MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
        }

but this code shows the temperature that is not the correct temperature. It ususally shows 49.5-50.5 degrees centigrade. But I used "OpenHardwareMonitor" that report CPU temperature over 71 degree centigrade and changing fractions along with time fractions. is there anything I am missing in the code?

I used the above code in timer_click event for every 500ms interval to refresh the temperature reading but it's always showing the same temperature from the beginning of execution. That implies if you run this application and if it shows 49 degree then after 1 hour session, it'll constantly show 49 degree. Where is the problem?

  • Does the value of `queryObj["ThermalStamp"]` change between calls? – Frédéric Hamidi Feb 01 '12 at 18:42
  • @FrédéricHamidi--I checked ThermalStamp property and it gets 8 value in call but never changes between calls... –  Feb 01 '12 at 18:58
  • Strange. Are you definitely sure your timer ticks when you think it does and that your code is called on each tick? – Frédéric Hamidi Feb 01 '12 at 19:01
  • the output is in 10ths of Kelvin so you would have to convert it to Celcius – MethodMan Feb 01 '12 at 19:04
  • @FrédéricHamidi--yes, i have checked/debugged the app several times. timer is fine and it ticks every 500 ms and call the above code, exactly working well but still nothing is updated... –  Feb 01 '12 at 19:07
  • @DJKRAZE..yeah, you are right. I have converted the temp. reading to degree centigrade but it's not get updated every 500ms –  Feb 01 '12 at 19:08
  • 1
    This only works on laptops. For 99% of desktops, manufacturers do not implement ACPI temperature. – leppie Feb 01 '12 at 19:26

1 Answers1

6

In https://web.archive.org/web/20150911113852/http://www.scriptinternals.com/new/us/support/Internal/WMI_MSAcpi_ThermalZoneTemperature.htm I've found that CurrentTemperature returns temperature at some thermal zone which is somewhere on motherboard. That means it returns not CPU temperature. It would be the same as temperature in the kitchen is 30C but stove is 200C or so... This way cannot show exact temperature of CPU. To get the exact temperature of CPU (and every core) you need to write kernel drivers, what is much more complicated.

All-in-all your code does that it should do, for taking temperature you need to use some other way.

mklement0
  • 382,024
  • 64
  • 607
  • 775
ST3
  • 8,826
  • 3
  • 68
  • 92
  • 1
    Yes, you are right. Thanks for the reply. I have figured it out later that CPU temperatures can not be determined using Microsoft's WMI class. –  Jun 25 '13 at 07:28
  • @jchoudhury Just want to ask, have you find some easy way to get that temperatures? – ST3 Jul 06 '13 at 20:21
  • i didn't find it yet. If you have any solution better than WMI, then please share it with me. Thanks for asking though. –  Aug 16 '13 at 02:27
  • Speedfan is the most reliable solution I've found. https://www.almico.com/sfdownload.php – trindflo Aug 03 '23 at 22:57