1.1 --- a/Hardware/CPU/CPULoad.cs Thu Aug 12 20:53:27 2010 +0000
1.2 +++ b/Hardware/CPU/CPULoad.cs Sun Aug 15 14:46:58 2010 +0000
1.3 @@ -62,12 +62,6 @@
1.4 SystemProcessorPerformanceInformation = 8
1.5 }
1.6
1.7 - [DllImport("ntdll.dll")]
1.8 - private static extern int NtQuerySystemInformation(
1.9 - SystemInformationClass informationClass,
1.10 - [Out] SystemProcessorPerformanceInformation[] informations,
1.11 - int structSize, out IntPtr returnLength);
1.12 -
1.13 private CPUID[][] cpuid;
1.14
1.15 private long systemTime;
1.16 @@ -78,16 +72,17 @@
1.17
1.18 private bool available = false;
1.19
1.20 - private long[] GetIdleTimes() {
1.21 + private static long[] GetIdleTimes() {
1.22 SystemProcessorPerformanceInformation[] informations = new
1.23 SystemProcessorPerformanceInformation[64];
1.24
1.25 int size = Marshal.SizeOf(typeof(SystemProcessorPerformanceInformation));
1.26
1.27 IntPtr returnLength;
1.28 - NtQuerySystemInformation(
1.29 + if (NativeMethods.NtQuerySystemInformation(
1.30 SystemInformationClass.SystemProcessorPerformanceInformation,
1.31 - informations, informations.Length * size, out returnLength);
1.32 + informations, informations.Length * size, out returnLength) != 0)
1.33 + return null;
1.34
1.35 long[] result = new long[(int)returnLength / size];
1.36
1.37 @@ -127,10 +122,13 @@
1.38 if (this.idleTimes == null)
1.39 return;
1.40
1.41 - long localSystemTime = DateTime.Now.Ticks;
1.42 - long[] localIdleTimes = GetIdleTimes();
1.43 + long newSystemTime = DateTime.Now.Ticks;
1.44 + long[] newIdleTimes = GetIdleTimes();
1.45
1.46 - if (localSystemTime - this.systemTime < 10000)
1.47 + if (newSystemTime - this.systemTime < 10000)
1.48 + return;
1.49 +
1.50 + if (newIdleTimes == null)
1.51 return;
1.52
1.53 float total = 0;
1.54 @@ -139,29 +137,37 @@
1.55 float value = 0;
1.56 for (int j = 0; j < cpuid[i].Length; j++) {
1.57 long index = cpuid[i][j].Thread;
1.58 - if (index < localIdleTimes.Length) {
1.59 - long delta = localIdleTimes[index] - this.idleTimes[index];
1.60 + if (index < newIdleTimes.Length) {
1.61 + long delta = newIdleTimes[index] - this.idleTimes[index];
1.62 value += delta;
1.63 total += delta;
1.64 count++;
1.65 }
1.66 }
1.67 value = 1.0f - value / (cpuid[i].Length *
1.68 - (localSystemTime - this.systemTime));
1.69 + (newSystemTime - this.systemTime));
1.70 value = value < 0 ? 0 : value;
1.71 coreLoads[i] = value * 100;
1.72 }
1.73 if (count > 0) {
1.74 - total = 1.0f - total / (count * (localSystemTime - this.systemTime));
1.75 + total = 1.0f - total / (count * (newSystemTime - this.systemTime));
1.76 total = total < 0 ? 0 : total;
1.77 } else {
1.78 total = 0;
1.79 }
1.80 this.totalLoad = total * 100;
1.81
1.82 - this.systemTime = localSystemTime;
1.83 - this.idleTimes = localIdleTimes;
1.84 + this.systemTime = newSystemTime;
1.85 + this.idleTimes = newIdleTimes;
1.86 }
1.87
1.88 + private static class NativeMethods {
1.89 +
1.90 + [DllImport("ntdll.dll")]
1.91 + public static extern int NtQuerySystemInformation(
1.92 + SystemInformationClass informationClass,
1.93 + [Out] SystemProcessorPerformanceInformation[] informations,
1.94 + int structSize, out IntPtr returnLength);
1.95 + }
1.96 }
1.97 }