我想知道是否有人可以帮助获取特定应用程序的cpu使用信息,我能够使用以下code
获取与您在任务管理器中看到的相同的使用信息任何帮助将不胜感激, 感谢。
答案 0 :(得分:0)
有两种方法可以做到这一点。
首先是使用WMI
var searcher = new ManagementObjectSearcher("select * from Win32_PerfFormattedData_PerfOS_Processor");
var cpuUsages = searcher.Get()
.Cast<ManagementObject>()
.Select(x => new
{
Name = x["Name"],
Usage = x["PercentProcessorTime"]
}
)
.ToList();
var totalUsage = cpuUsages.Where(x => x.Name.ToString() == "_Total").Select(x => x.Usage).SingleOrDefault();
第二种是使用PerformanceCounter foreach核心。
int processorCores = new ManagementObjectSearcher("Select * from Win32_Processor").Get().Cast<ManagementBaseObject>().Sum(item => int.Parse(item["NumberOfCores"].ToString()));
var performanceCounter = new PerformanceCounter[processorCores];
for (var i = 0; i < processorCores; i++)
{
performanceCounter[i] = new PerformanceCounter("Processor", "% Processor Time", i.ToString());
}
答案 1 :(得分:0)