C#监视CPU使用率与任务管理器

时间:2018-07-05 21:16:24

标签: c# cpu performancecounter

我正在用c#监视CPU使用率,但是总CPU使用率的值总是比任务管理器显示的值少15%。有人知道为什么会这样吗?

private static PerformanceCounter cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
private static PerformanceCounter ramCounter = new PerformanceCounter("Memory", "Available MBytes");

public static void LogPerformance()
{            
    string dateString = DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss") + " - ";
    string cpuUsage = dateString + getCurrentCpuUsage() + Environment.NewLine;
    string ramUsage = dateString + getAvailableRAM() + Environment.NewLine;
    Console.WriteLine(cpuUsage);
    File.AppendAllText(cpuFile, cpuUsage);
    File.AppendAllText(ramFile, ramUsage);
}

private static string getCurrentCpuUsage()
{
    return cpuCounter.NextValue() + "%";
}

private static string getAvailableRAM()
{
    return ramCounter.NextValue() + "MB";
}

0 个答案:

没有答案