Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to have a read out of the CPU temperature and load in one form. I have managed to get this to work with the CPU load, however I am stuck when trying to get results with the CPU temperature and I keep receiving a "System.ArgumentOutOfRangeException" error on the line: "ManagementObject temperature = new ManagementObject("MSAcpi_ThermalZoneTemperature");" When opening the form.

here is my code:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;




namespace CPUForm
{
    public class CPUPage : Form
    {
        public CPUPage()
        {
            InitializeComponent();
            timer1.Enabled = true;
            timer1.Interval = 1000;
           CPULOAD();
            CPUTemperature();
        }

      public void CPULOAD()
        {
            ulong CPULOAD;
            ManagementObject processor = new ManagementObject(
            "Win32_PerfFormattedData_PerfOS_Processor.Name='_Total'");
            processor.Get();
            CPULOAD = (ulong)processor.Properties["PercentProcessorTime"].Value;
            label2.Text = CPULOAD.ToString() + "%";

        } 
        public void CPUTemperature()
       {
           ulong CPUTemperature;
           ManagementObject temperature = new ManagementObject(
          "MSAcpi_ThermalZoneTemperature");
           temperature.Get();
           CPUTemperature = (ulong)temperature.Properties["CurrentTemperature"].Value;
           label3.Text = CPUTemperature.ToString();
       }
        private void timer1_Tick(object sender, EventArgs e)
        {

            CPULOAD();
            CPUTemperature();
        }
        private Label label1;

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.timer1 = new System.Windows.Forms.Timer(this.components);
            this.label3 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(25, 164);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(61, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "CPU LOAD";
            // 
            // label2
            // 
            this.label2.BackColor = System.Drawing.SystemColors.Window;
            this.label2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label2.ForeColor = System.Drawing.SystemColors.ActiveCaption;
            this.label2.Location = new System.Drawing.Point(28, 31);
            this.label2.Name = "label2";
            this.label2.Padding = new System.Windows.Forms.Padding(32, 6, 0, 0);
            this.label2.Size = new System.Drawing.Size(109, 121);
            this.label2.TabIndex = 2;
            this.label2.Text = "0%";
            // 
            // timer1
            // 
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick_1);
            // 
            // label3
            // 
            this.label3.BackColor = System.Drawing.SystemColors.Window;
            this.label3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label3.ForeColor = System.Drawing.SystemColors.ActiveCaption;
            this.label3.Location = new System.Drawing.Point(244, 31);
            this.label3.Name = "label3";
            this.label3.Padding = new System.Windows.Forms.Padding(32, 6, 0, 0);
            this.label3.Size = new System.Drawing.Size(201, 146);
            this.label3.TabIndex = 3;
            // 
            // CPUPage
            // 
            this.ClientSize = new System.Drawing.Size(624, 266);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Name = "CPUPage";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private Label label2;
        private Timer timer1;
        private IContainer components;
        private Label label3;

        private void timer1_Tick_1(object sender, EventArgs e)
        {
           CPULOAD();
            CPUTemperature();
        }
    }
}
Posted

1 solution

Two problems.

First, the MSAcpi_ThermalZoneTemperature class is in the WMI "root\WMI" namespace. You're using the "root\cimv2" namespace since you're not specifying one in your WMI code.

Second, not all motherboards support reporting CPU temp through WMI. There is no way to tell without just trying it to see if it works. If it doesn't, go back to your motherboard manufacturer and see if they provide WMI drivers. If not, you're out of luck.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900