diff --git a/Hardware/CPU/AMD0FCPU.cs b/Hardware/CPU/AMD0FCPU.cs index 785bc5a..bc35837 100644 --- a/Hardware/CPU/AMD0FCPU.cs +++ b/Hardware/CPU/AMD0FCPU.cs @@ -80,19 +80,29 @@ namespace OpenHardwareMonitor.Hardware.CPU { // max two cores coreCount = coreCount > 2 ? 2 : coreCount; - totalLoadCounter = new PerformanceCounter(); - totalLoadCounter.CategoryName = "Processor"; - totalLoadCounter.CounterName = "% Processor Time"; - totalLoadCounter.InstanceName = "_Total"; + try { + totalLoadCounter = new PerformanceCounter(); + totalLoadCounter.CategoryName = "Processor"; + totalLoadCounter.CounterName = "% Processor Time"; + totalLoadCounter.InstanceName = "_Total"; + totalLoadCounter.NextValue(); + } catch (Exception) { + totalLoadCounter = null; + } totalLoad = new Sensor("CPU Total", 0, SensorType.Load, this); coreLoadCounters = new PerformanceCounter[coreCount]; coreLoads = new Sensor[coreCount]; for (int i = 0; i < coreLoadCounters.Length; i++) { - coreLoadCounters[i] = new PerformanceCounter(); - coreLoadCounters[i].CategoryName = "Processor"; - coreLoadCounters[i].CounterName = "% Processor Time"; - coreLoadCounters[i].InstanceName = i.ToString(); + try { + coreLoadCounters[i] = new PerformanceCounter(); + coreLoadCounters[i].CategoryName = "Processor"; + coreLoadCounters[i].CounterName = "% Processor Time"; + coreLoadCounters[i].InstanceName = i.ToString(); + coreLoadCounters[i].NextValue(); + } catch (Exception) { + coreLoadCounters[i] = null; + } coreLoads[i] = new Sensor("Core #" + (i + 1), i + 1, SensorType.Load, this); } @@ -153,13 +163,16 @@ namespace OpenHardwareMonitor.Hardware.CPU { } } - totalLoad.Value = totalLoadCounter.NextValue(); - ActivateSensor(totalLoad); - - for (int i = 0; i < coreLoads.Length; i++) { - coreLoads[i].Value = coreLoadCounters[i].NextValue(); - ActivateSensor(coreLoads[i]); + if (totalLoadCounter != null) { + totalLoad.Value = totalLoadCounter.NextValue(); + ActivateSensor(totalLoad); } + + for (int i = 0; i < coreLoads.Length; i++) + if (coreLoadCounters[i] != null) { + coreLoads[i].Value = coreLoadCounters[i].NextValue(); + ActivateSensor(coreLoads[i]); + } } private void ActivateSensor(Sensor sensor) { diff --git a/Hardware/CPU/AMD10CPU.cs b/Hardware/CPU/AMD10CPU.cs index a9e5056..3f96ed5 100644 --- a/Hardware/CPU/AMD10CPU.cs +++ b/Hardware/CPU/AMD10CPU.cs @@ -72,19 +72,29 @@ namespace OpenHardwareMonitor.Hardware.CPU { if (cpuidExtData.GetLength(0) > 8) coreCount = (cpuidExtData[8, 2] & 0xFF) + 1; - totalLoadCounter = new PerformanceCounter(); - totalLoadCounter.CategoryName = "Processor"; - totalLoadCounter.CounterName = "% Processor Time"; - totalLoadCounter.InstanceName = "_Total"; + try { + totalLoadCounter = new PerformanceCounter(); + totalLoadCounter.CategoryName = "Processor"; + totalLoadCounter.CounterName = "% Processor Time"; + totalLoadCounter.InstanceName = "_Total"; + totalLoadCounter.NextValue(); + } catch (Exception) { + totalLoadCounter = null; + } totalLoad = new Sensor("CPU Total", 0, SensorType.Load, this); coreLoadCounters = new PerformanceCounter[coreCount]; coreLoads = new Sensor[coreCount]; for (int i = 0; i < coreLoadCounters.Length; i++) { - coreLoadCounters[i] = new PerformanceCounter(); - coreLoadCounters[i].CategoryName = "Processor"; - coreLoadCounters[i].CounterName = "% Processor Time"; - coreLoadCounters[i].InstanceName = i.ToString(); + try { + coreLoadCounters[i] = new PerformanceCounter(); + coreLoadCounters[i].CategoryName = "Processor"; + coreLoadCounters[i].CounterName = "% Processor Time"; + coreLoadCounters[i].InstanceName = i.ToString(); + coreLoadCounters[i].NextValue(); + } catch (Exception) { + coreLoadCounters[i] = null; + } coreLoads[i] = new Sensor("Core #" + (i + 1), i + 1, SensorType.Load, this); } @@ -132,13 +142,16 @@ namespace OpenHardwareMonitor.Hardware.CPU { DeactivateSensor(coreTemperature); } - totalLoad.Value = totalLoadCounter.NextValue(); - ActivateSensor(totalLoad); - - for (int i = 0; i < coreLoads.Length; i++) { - coreLoads[i].Value = coreLoadCounters[i].NextValue(); - ActivateSensor(coreLoads[i]); + if (totalLoadCounter != null) { + totalLoad.Value = totalLoadCounter.NextValue(); + ActivateSensor(totalLoad); } + + for (int i = 0; i < coreLoads.Length; i++) + if (coreLoadCounters[i] != null) { + coreLoads[i].Value = coreLoadCounters[i].NextValue(); + ActivateSensor(coreLoads[i]); + } } private void ActivateSensor(Sensor sensor) { diff --git a/Hardware/CPU/IntelCPU.cs b/Hardware/CPU/IntelCPU.cs index 6ebed8b..63e622a 100644 --- a/Hardware/CPU/IntelCPU.cs +++ b/Hardware/CPU/IntelCPU.cs @@ -133,19 +133,29 @@ namespace OpenHardwareMonitor.Hardware.CPU { default: tjMax = 100; break; } - totalLoadCounter = new PerformanceCounter(); - totalLoadCounter.CategoryName = "Processor"; - totalLoadCounter.CounterName = "% Processor Time"; - totalLoadCounter.InstanceName = "_Total"; + try { + totalLoadCounter = new PerformanceCounter(); + totalLoadCounter.CategoryName = "Processor"; + totalLoadCounter.CounterName = "% Processor Time"; + totalLoadCounter.InstanceName = "_Total"; + totalLoadCounter.NextValue(); + } catch (Exception) { + totalLoadCounter = null; + } totalLoad = new Sensor("CPU Total", 0, SensorType.Load, this); coreLoadCounters = new PerformanceCounter[ coreCount * logicalProcessorsPerCore]; for (int i = 0; i < coreLoadCounters.Length; i++) { - coreLoadCounters[i] = new PerformanceCounter(); - coreLoadCounters[i].CategoryName = "Processor"; - coreLoadCounters[i].CounterName = "% Processor Time"; - coreLoadCounters[i].InstanceName = i.ToString(); + try { + coreLoadCounters[i] = new PerformanceCounter(); + coreLoadCounters[i].CategoryName = "Processor"; + coreLoadCounters[i].CounterName = "% Processor Time"; + coreLoadCounters[i].InstanceName = i.ToString(); + coreLoadCounters[i].NextValue(); + } catch (Exception) { + coreLoadCounters[i] = null; + } } coreTemperatures = new Sensor[coreCount]; @@ -211,17 +221,27 @@ namespace OpenHardwareMonitor.Hardware.CPU { } } - totalLoad.Value = totalLoadCounter.NextValue(); - ActivateSensor(totalLoad); + if (totalLoadCounter != null) { + totalLoad.Value = totalLoadCounter.NextValue(); + ActivateSensor(totalLoad); + } for (int i = 0; i < coreLoads.Length; i++) { float value = 0; - for (int j = 0; j < logicalProcessorsPerCore; j++) - value += coreLoadCounters[ - logicalProcessorsPerCore * i + j].NextValue(); - value /= logicalProcessorsPerCore; - coreLoads[i].Value = value; - ActivateSensor(coreLoads[i]); + int count = 0; + for (int j = 0; j < logicalProcessorsPerCore; j++) { + PerformanceCounter counter = + coreLoadCounters[logicalProcessorsPerCore * i + j]; + if (counter != null) { + value += counter.NextValue(); + count++; + } + } + if (count > 0) { + value /= count; + coreLoads[i].Value = value; + ActivateSensor(coreLoads[i]); + } } } diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index 28740a3..a345be8 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -69,5 +69,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.1.11.0")] -[assembly: AssemblyFileVersion("0.1.11.0")] +[assembly: AssemblyVersion("0.1.12.0")] +[assembly: AssemblyFileVersion("0.1.12.0")]