void UpdatePerfomanceData_CPU() { PerfomanceInfo_CPU[] infos = GetPerfomanceInfo_CPU(DataHelper.WmiService); if (infos.Length == 1) { PerfomanceInfo_CPU info = GetBufferedPerfomanceInfo(infos[0]); DashboardGauge.Scales["cpuTotal"].Value = info.Total; DashboardGauge.Scales["cpuUser"].Value = info.Kernel + info.User; DashboardGauge.Scales["cpuKernel"].Value = info.Kernel; } }
static PerfomanceInfo_CPU[] GetPerfomanceInfo_CPU(WMIService wmiService) { ManagementObject[] collection = wmiService.GetObjects( "SELECT Name,PercentProcessorTime,PercentPrivilegedTime,PercentUserTime " + "FROM Win32_PerfFormattedData_PerfOS_Processor " + "WHERE Name=\'_Total\'", false ); PerfomanceInfo_CPU[] result = new PerfomanceInfo_CPU[collection.Length]; for (int i = 0; i < collection.Length; i++) { result[i] = new PerfomanceInfo_CPU( (string)collection[i].Properties["Name"].Value, (float)(UInt64)collection[i].Properties["PercentProcessorTime"].Value, (float)(UInt64)collection[i].Properties["PercentPrivilegedTime"].Value, (float)(UInt64)collection[i].Properties["PercentUserTime"].Value ); } return(result); }
PerfomanceInfo_CPU GetBufferedPerfomanceInfo(PerfomanceInfo_CPU currentValue) { for (int i = 1; i < buffer.Length; i++) { buffer[i - 1] = buffer[i]; } buffer[buffer.Length - 1] = currentValue; float total = 0; float kernel = 0; float user = 0; int n = 0; for (int i = 0; i < buffer.Length; i++) { if (buffer[i] != null) { total += buffer[i].Total; kernel += buffer[i].Kernel; user += buffer[i].User; n++; } } return(new PerfomanceInfo_CPU(currentValue.Name, total / (float)n, kernel / (float)n, user / (float)n)); }