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:
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();
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";
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%";
this.timer1.Tick += new System.EventHandler(this.timer1_Tick_1);
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;
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();
}
}
}