【问题标题】:With Process PerformanceCounters how do I know what process an instance is associated with?使用 Process PerformanceCounters 我如何知道实例与哪个进程相关联?
【发布时间】:2010-09-09 17:58:49
【问题描述】:

在查询“进程”性能计数器类别的实例时,可能存在同名进程的多个实例。

例如这段代码:

var cat = new PerformanceCounterCategory("Process");

var names = cat.GetInstanceNames();

foreach (var name in names)
    Console.WriteLine(name);

可能会打印这些结果: ... 探索 探索#1 探索#2 探索#3 ...

我如何知道每个计数器实例对应于哪个进程?

【问题讨论】:

    标签: c# .net performancecounter


    【解决方案1】:

    “Process”类别中有一个名为“ID Process”的PerformanceCounter,它将返回性能计数器实例对应的进程的pid。

    var cat = new PerformanceCounterCategory("Process");
    
    var names = cat.GetInstanceNames();
    
    foreach (var name in names.OrderBy(n => n))
    {
        var pidCounter = new PerformanceCounter("Process", "ID Process", name, true);
        var sample = pidCounter.NextSample();
        Console.WriteLine(name + ": " + sample.RawValue);
    }
    

    这将打印:

    ...

    探索:548

    iexplore#1:1268

    iexplore#2: 4336

    ...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-08
      • 2012-07-15
      • 1970-01-01
      • 1970-01-01
      • 2021-10-31
      • 1970-01-01
      • 2018-07-19
      • 1970-01-01
      相关资源
      最近更新 更多