openhardware monitor exception

439 Views Asked by At

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 :

enter image description here

enter image description here

1

There are 1 best solutions below

4
James Gould On

Remove the myComputer.Close(); from your foreach loop and put if afterwards. You're closing myComputer after the first iteration of your loop and then trying to access it again, hence your ArgumentOutOfRangeException.

Also not sure if its a typo or if it's part of your code, but you shouldn't have enter code here in the middle of your method :-)