我目前使用此代码,让我的CPU在四个核的使用值,并显示每一个以他们自己的标签:为什么使用WMI获取CPU使用冻结我的程序几秒钟?
string[] cpuUsage = new string[100];
int i = 0;
//Get CPU usage values using WMI
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_PerfFormattedData_PerfOS_Processor");
foreach (ManagementObject obj in searcher.Get())
{
cpuUsage[i] = obj["PercentProcessorTime"].ToString();
i++;
}
LA_Core1.Content = "Core usage: " + cpuUsage[0] + "%";
LA_Core2.Content = "Core usage: " + cpuUsage[1] + "%";
LA_Core3.Content = "Core usage: " + cpuUsage[2] + "%";
LA_Core4.Content = "Core usage: " + cpuUsage[3] + "%";
但是,对于第一次运行这些代码,它冻结我的程序在大约10秒钟后才显示出来。然而,这是第一次,代码立即运行。到底发生了什么事情,使其在第一次运行时变得如此缓慢,而不是随后的运行?
我确实看到了这样的延迟。不可能钉上它,但它重复得太差。改为考虑PerformanceCounter。 – 2014-09-21 12:31:27
我想我可能只需要欢呼。 – dantdj 2014-09-21 12:31:51