【问题标题】:Multiple Processors and PerformanceCounter C#多处理器和 PerformanceCounter C#
【发布时间】:2011-01-15 00:34:53
【问题描述】:

我试图弄清楚如何收集我计算机上每个处理器的当前使用百分比。如果我使用“System.Environment.ProcessorCount;”我可以获取计算机上的处理器数量,它当前返回“2”。我要么不知道我在寻找什么,要么互联网上没有太多关于此的信息。

以下是我当前用于获取所有处理器组合的当前总使用百分比的代码。

protected PerformanceCounter cpuCounter = new PerformanceCounter("processor", "% Processor Time", "_Total");
public string getCurrentCpuUsage()
{
    return cpuCounter.NextValue() + "%";
}

感谢您的帮助,

亚伦

【问题讨论】:

    标签: c# performancecounter processor


    【解决方案1】:

    对于第一个处理器,使用

    protected PerformanceCounter cpuCounter = 
      new PerformanceCounter("processor", "% Processor Time", "0");
    

    以此类推,直到 (Environment.ProcessorCount-1).ToString()

    【讨论】:

      【解决方案2】:

      自从我留下最初的问题后,我碰巧在我的 Windows 7 计算机上找到了 Windows 性能监视器 (C:\Windows\system32\perfmon.msc)。如果右键单击主窗口上的图表并选择“添加计数器”,则会显示一个可能的字符串列表,可用作 PerformanceCounter 中的参数,用于几乎所有想要监控的内容。

      【讨论】:

      • 这是一座金矿。感谢您的贡献,至今仍在帮助!
      • 非常感谢!这正是我想要的。
      猜你喜欢
      • 1970-01-01
      • 2011-12-31
      • 1970-01-01
      • 2012-05-11
      • 2018-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-29
      相关资源
      最近更新 更多