我想显示我的多线程应用程序的CPU使用率(在多核处理器上工作).我想收到任务经理附近的数字.但我得到的数字超过100%.甚至超过500%.是的,我知道,对于类别"进程"的计数器"%Processor Time",我需要分为Environment.ProcessorCount或"NumberOfLogicalProcessors"(我的配置相同).此操作后结果为500%.我在具有不同硬件(i7,i5,Core2)和软件配置(带有所有更新的Windows 7 SP1,带有所有更新的Windows 2008 R2 SP1)的不同计算机上测试了此示例,并遇到了同样的问题.
public static class SystemInfo
{
private static Process _thisProc;
private static bool HasData = false;
private static PerformanceCounter _processTimeCounter;
private static void Init()
{
if (HasData)
return;
if (CheckForPerformanceCounterCategoryExist("Process"))
{
_processTimeCounter = new PerformanceCounter();
_processTimeCounter.CategoryName = "Process";
_processTimeCounter.CounterName = "% Processor Time";
_processTimeCounter.InstanceName = FindInstanceName("Process");
_processTimeCounter.NextValue();
}
MaximumCpuUsageForCurrentProcess = 0;
HasData = true;
}
private static bool CheckForPerformanceCounterCategoryExist(string categoryName)
{
return PerformanceCounterCategory.Exists(categoryName);
} …Run Code Online (Sandbox Code Playgroud) 我想使用.NET/Process性能计数器在网页上显示一些内存统计信息(工作集,GC等).不幸的是,如果该服务器上有多个应用程序池,则使用索引(#1,#2等)对它们进行区分,但我不知道如何将进程ID(我有)与该#xx索引进行匹配.是否有编程方式(来自ASP.NET网页)?
我正在使用这个类作为一类测试的基类,这些测试启动一个进程并给它一些输入,并在给它更多输入之前等待它变为空闲.
public abstract class TestProcessLaunchingBase
{
protected PerformanceCounter PerfCounter { get; set; }
protected void WaitForProcessIdle()
{
while (true)
{
float oldValue = PerfCounter.NextValue();
Thread.Sleep(1000);
float nextValue = PerfCounter.NextValue();
if (nextValue == 0)
break;
}
}
protected void FindSpawnedProcessPerfCounter(int processId)
{
PerformanceCounterCategory cat = new PerformanceCounterCategory("Process");
string[] instances = cat.GetInstanceNames();
foreach (string instance in instances)
{
using (PerformanceCounter cnt = new PerformanceCounter("Process", "ID Process", instance, true))
{
int val = (int)cnt.RawValue;
if (val == processId)
{
PerfCounter = new PerformanceCounter("Process", …Run Code Online (Sandbox Code Playgroud)