I guess the problem is the too much foreach loops there are.
But i need them to get the sensor.Value
This is the two functions:
private void cpuView()
{
Computer myComputer = new Computer();
myComputer = new Computer(settings) { CPUEnabled = true };
myComputer.Open();
Trace.WriteLine("");
foreach (var hardwareItem in myComputer.Hardware)
{
if (hardwareItem.HardwareType == HardwareType.CPU)
{
hardwareItem.Update();
foreach (IHardware subHardware in hardwareItem.SubHardware)
subHardware.Update();
foreach (var sensor in hardwareItem.Sensors)
{
settings.SetValue("sensor", sensor.Value.ToString());
if (sensor.SensorType == SensorType.Temperature)
{
sensor.Hardware.Update();
settings.GetValue("sensor", sensor.Value.ToString());
temperature_label.Text = sensor.Value.ToString() + "c";//String.Format("{0} Temperature = {1}c", sensor.Name, sensor.Value.HasValue ? sensor.Value.Value.ToString() : "no value");
}
}
}
}
}
And the second function:
private void gpuView()
{
Computer computer = new Computer();
computer.Open();
computer.GPUEnabled = true;
foreach (var hardwareItem in computer.Hardware)
{
if (videoCardType("ati", "nvidia") == true)
{
HardwareType htype = HardwareType.GpuNvidia;
if (hardwareItem.HardwareType == htype)
{
foreach (var sensor in hardwareItem.Sensors)
{
if (sensor.SensorType == SensorType.Temperature)
{
sensor.Hardware.Update();
if (sensor.Value.ToString().Length > 0)
{
if (newGPULabel.Text.Length < 1)
{
if (UpdatingLabel(sensor.Value.ToString(), string.Empty))
{
label8.Text = newGPULabel.Text;
}
}
else if (UpdatingLabel(sensor.Value.ToString(), newGPULabel.Text.Substring(0, newGPULabel.Text.Length - 1)))
{
label8.Text = newGPULabel.Text;
}
newGPULabel.Text = sensor.Value.ToString() + "c";
label8.Visible = true;
}
int t = newGPULabel.Text.Length;
if (t >= 4)
{
newGPULabel.Location = new Point(210, 100);
}
else
{
newGPULabel.Location = new Point(250, 100);
}
timer2.Interval = 1000;
if (sensor.Value > 90)
{
Logger.Write("The current temperature is ===> " + sensor.Value);
button1.Enabled = true;
}
this.Select();
}
}
}
}
else
{
HardwareType htype = HardwareType.GpuAti;
if (hardwareItem.HardwareType == htype)
{
foreach (var sensor in hardwareItem.Sensors)
{
if (sensor.SensorType == SensorType.Temperature)
{
sensor.Hardware.Update();
if (sensor.Value.ToString().Length > 0)
{
if (newGPULabel.Text.Length < 1)
{
if (UpdatingLabel(sensor.Value.ToString(), string.Empty))
{
label8.Text = newGPULabel.Text;
}
}
else if (UpdatingLabel(sensor.Value.ToString(), newGPULabel.Text.Substring(0, newGPULabel.Text.Length - 1)))
{
label8.Text = newGPULabel.Text;
}
newGPULabel.Text = sensor.Value.ToString() + "c";
label8.Visible = true;
}
int t = newGPULabel.Text.Length;
if (t >= 4)
{
newGPULabel.Location = new Point(210, 100);
}
else
{
newGPULabel.Location = new Point(250, 100);
}
timer2.Interval = 1000;
if (sensor.Value > 90)
{
Logger.Write("The current temperature is ===> " + sensor.Value);
button1.Enabled = true;
}
this.Select();
}
}
}
}
}
}
And in the timer2 tick event:
private void timer2_Tick(object sender, EventArgs e)
{
gpuView();
cpuView();
}
If i dont call this functions in the tick event the program is running smooth but even if im calling only one of the functions its stuck each n seconds.
The timer2 is set to interval 100 since im updating the cpu and gpu temperature i want to update them fast.
Im not sure if the problem is the too much foreach loops in the functions or that the interval is 100 so its trying to get the values from the sensors too fast so the hardware cant sned the information too fast.
Maybe there is a way to remove some of the foreach loops ?
I dont want to upload all my Form1 code to here . But all the snesors and hardware are connecting with the OpenHardwareMonitor.dll of the OpenHardwareMonitorApplication.
You can use a backgroundworker,it allow you doing multithreading on an easy way.
it will be useful on your case and call doWork method on asynchronous way.In this method call your two functions on a loop.
Here msdn explanation http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx
it is better than a timer that you set an random interval value because you have to let your functions complete so if an event have to occur it won’t ….