PerformanceCounter 好像不能获得多CPU使用率,在单CPU上跑正常,多CPU上一直是0,
有什么方法能获得多CPU或多核下,CUP使用率,有代码满分相送!

解决方案 »

  1.   

    http://www.codeproject.com/KB/system/CpuUsage.aspx
    这个可能可以帮助你
      

  2.   

    performance counters 应该可以
    这是我在msdn论坛上找到的话(原文http://forums.msdn.microsoft.com/zh-CN/csharpgeneral/thread/ebf104df-3328-47b8-9474-c1f781e49ab9/)
    You can use performance counters (System.Diagnostics.PerformanceCounter).  There are counter instances for each individual CPU, and you can sample them separately.  You can also obtain the list of instances by using PerformanceCounterCategory.GetInstancesNames, or you can rely on Environment.ProcessorCount to determine how many instances you need to enumerate (I recommend the former approach).
      

  3.   

    使用PerformanceCounter 获取多核CPU使用率绝对没问题performanceCounter1.CounterName = "% Committed Bytes In Use";
    float i = performanceCounter1.NextValue();
      

  4.   

    多个CPU也是没问题的..
    但是,如果使用PerformanceCounter 获取单个进程的CPU使用率的话,还需要除以CPU个数:
    float i = performanceCounter1.NextValue() / NumOfCPU;
      

  5.   

    刚才说错了.应该是"% Processor Time".
      

  6.   

    要设置PerformanceCounter的InstanceName,cpu1: '1', cpu2: '2', ..., total: '_Total'
    using System;
    using System.Diagnostics;class Program
    {
        static void Main(string[] args)
        {
            PerformanceCounter[] counters = new PerformanceCounter[ System.Environment.ProcessorCount ];
            for(int i=0; i<counters.Length; i++)
            {
                counters[i] = new PerformanceCounter("Processor", "% Processor Time", i.ToString());
            }        while(true)
            {
                for(int i=0; i<counters.Length; i++)
                {
                    float f = counters[i].NextValue();
                    Console.WriteLine("CPU-{0}: {1:f}%", i, f);
                }
                Console.WriteLine();
                System.Threading.Thread.Sleep(1000);
            }
        }
    }
      

  7.   

    while(true)是什么意思?这样写循环还出得来吗?
      

  8.   

    而且"Processor", "% Processor Time"得到得值都是0