My program is giving me two exceptions when running open ().
I already did a lot of search and I could not solve it. Someone can help me please, I am enthusiastic in programming. So, I do not know very well to solve these errors.
public void tempcpu()
{
myComputer = new Computer()
{
MainboardEnabled = true,
CPUEnabled = true,
RAMEnabled = true,
GPUEnabled = true,
FanControllerEnabled = true,
HDDEnabled = true
};
myComputer.Open();
float averange = 0;
foreach (var hardware in myComputer.Hardware)
{
if (hardware.HardwareType == HardwareType.CPU)
{
hardware.Update();
foreach (var sensor in (hardware.Sensors))enter code here
if (sensor.SensorType == SensorType.Temperature)
{if(sensor.Value!= null)
averange = averange + sensor.Value.Value;
}
}
cpu = averange / 5;
myComputer.Close();
}
}
Exceptions :


Remove the
myComputer.Close();from yourforeachloop and put if afterwards. You're closingmyComputerafter the first iteration of your loop and then trying to access it again, hence yourArgumentOutOfRangeException.Also not sure if its a typo or if it's part of your code, but you shouldn't have
enter code herein the middle of your method :-)