Why when trying to get my CPU temperature the variable sensor.Value is null ?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
In my top of Form1 im doing:
using OpenHardwareMonitor.Hardware;
I also referenced the OpenHardwareMonitorLib.dll
Then I have this method:
private void cpuView()
{
if (pauseContinueDoWork == true)
{
}
else
{
Computer myComputer = new Computer();
myComputer = new Computer(settings) { CPUEnabled =
true };

myComputer.Open();
myComputer.CPUEnabled = true;
Trace.WriteLine("");
foreach (var hardwareItem in myComputer.Hardware)
{
if (hardwareItem.HardwareType == HardwareType.
CPU)
{
hardwareItem.Update();
foreach (IHardware subHardware in hardwareIt
em.SubHardware)
subHardware.Update();

foreach (var sensor in hardwareItem.Sensors)
{
settings.SetValue("sensor", sensor.Value
.ToString());
if (sensor.SensorType == SensorType.Temp
erature)
{
sensor.Hardware.Update();
settings.GetValue("sensor", sensor.V
alue.ToString());
//label17.Text = sensor.Value.ToStri
ng() + "c";//String.Format("{0} Temperature = {1}c", sensor.Name, sensor.Value.HasValue ? sensor.Value.Value.ToString() : "no value");
if (InvokeRequired)
this.Invoke(new Action(() => labe
l17.Text = sensor.Value.ToString() + "c"));
else
label17.Text = sensor.Value.ToSt
ring() + "c";
tempCpuValue = sensor.Value;
if (sensor.Value > 60)
{
Logger.Write("The Current CPU Te
mperature Is ===> " + sensor.Value);
button1.Enabled = true;
}
int t = label17.Text.Length;
if (t >= 4)
{
if (InvokeRequired)
this.Invoke(new Action(() =>
label17.Location = new Point(50,20)));

}
else
{
this.Invoke(new Action(() => labe
l17.Location = new Point(50, 20)));
}

break;
}
}
}
}
}
}
I used a breakpoint on the line: settings.GetValue("sensor", sensor.Value.ToString());
And Value of sensor is null. sensor.Value so Value is null all the time.
And it worked for me before few hours ago I cant figure out what have been changed.
I can monitor no problems my GPU/Video card temperature but not the CPU.
And this CpuTemeprature class:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using OpenHardwareMonitor.Hardware;

namespace HardwareMonitoring
{
class CpuTemperature:ISettings
{



public CpuTemperature()
{

}

private IDictionary<string, string> settings = new Dictionary
<string, string>();

public CpuTemperature(IDictionary<string, string> settings)
{
this.settings = settings;

}

public bool Contains(string name)
{
return settings.ContainsKey(name);
}

public string GetValue(string name, string value)
{
string result;
if (settings.TryGetValue(name, out result))
return result;
else
return value;
}

public void Remove(string name)
{
settings.Remove(name);
}

public void SetValue(string name, string value)
{
settings[name] = value;
}

}
}What could be the reason that its null now ? Maybe i have changed something but cant figure out what it could be. Im pretty sure i didnt touch this part of the code.

View the full article
 
Back
Top