通过进程ID,而不是名称性能计数器? [英] Performance Counter by Process ID instead of name?

查看:171
本文介绍了通过进程ID,而不是名称性能计数器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跟踪同一应用程序的多个实例,并需要获得内存和CPU使用两种工艺。不过,我似乎无法找出一种方法使用性能计数器,知道这结果是哪个进程。我已经看到,我可以追加#1,这样的名称来获得每个结果的结尾,但是这并没有告诉我哪一个是为其过程。



如何确定进程ID或进程ID传递到柜台得到每同名?


$ B每一个过程的结果$ b

  PerformanceCounterCPU.CategoryName =过程; 
PerformanceCounterCPU.CounterName =%处理器时间;
PerformanceCounterCPU.InstanceName = proc.ProcessHandle.ProcessName;

PerformanceCounterMemory.CategoryName =过程;
PerformanceCounterMemory.CounterName =工作集 - 私人;
PerformanceCounterMemory.InstanceName = proc.ProcessHandle.ProcessName;


解决方案

这个答案以一个相关的问题可能的工作:

 私有静态字符串GetProcessInstanceName (INT PID)
{
PerformanceCounterCategory猫=新PerformanceCounterCategory(过程);

的String []实例= cat.GetInstanceNames();
的foreach(以实例的字符串实例)
{
$ B $使用B(的PerformanceCounter CNT =新的PerformanceCounter(过程,
标识过程,例如,真))
{
INT VAL =(INT)cnt.RawValue;
如果(VAL == PID)
{
返回实例;
}
}
}
抛出新的异常(找不到性能计数器+
为当前流程实例的名称。这是真正奇怪的...... );
}


I am tracking multiple instances of the same application and need to get the memory and cpu use of both processes. However, I cant seem to figure out a way to use the performance counter and know which result is for which process. I have seen that I can append #1 and such to the end of the name to get results for each, but that doesn't tell me which one is for which process.

How can I determine the ProcessId or pass the process ID to the counter to get the result per each process with same name?

PerformanceCounterCPU.CategoryName = "Process";
PerformanceCounterCPU.CounterName = "% Processor Time";
PerformanceCounterCPU.InstanceName = proc.ProcessHandle.ProcessName;

PerformanceCounterMemory.CategoryName = "Process";
PerformanceCounterMemory.CounterName = "Working Set - Private";
PerformanceCounterMemory.InstanceName = proc.ProcessHandle.ProcessName;

解决方案

This answer to a related question might work:

private static string GetProcessInstanceName(int pid)
{
  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 == pid)
        {
           return instance;
        }
     }
  }
  throw new Exception("Could not find performance counter " + 
      "instance name for current process. This is truly strange ...");
}

这篇关于通过进程ID,而不是名称性能计数器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆