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).
使用PerformanceCounter 获取多核CPU使用率绝对没问题performanceCounter1.CounterName = "% Committed Bytes In Use"; float i = performanceCounter1.NextValue();
多个CPU也是没问题的.. 但是,如果使用PerformanceCounter 获取单个进程的CPU使用率的话,还需要除以CPU个数: float i = performanceCounter1.NextValue() / NumOfCPU;
刚才说错了.应该是"% Processor Time".
要设置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); } } }
这个可能可以帮助你
这是我在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).
float i = performanceCounter1.NextValue();
但是,如果使用PerformanceCounter 获取单个进程的CPU使用率的话,还需要除以CPU个数:
float i = performanceCounter1.NextValue() / NumOfCPU;
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);
}
}
}