在查询“进程”性能计数器类别的实例时,可能存在多个具有相同名称的进程实例。
例如这段代码:
var cat = new PerformanceCounterCategory("Process");
var names = cat.GetInstanceNames();
foreach (var name in names)
Console.WriteLine(name);
可能会打印这些结果: ... IEXPLORE IEXPLORE#1 IEXPLORE#2 IEXPLORE#3 ...
我如何知道每个计数器实例对应哪个进程?
答案 0 :(得分:2)
“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);
}
这将打印:
...
iexplore:548
iexplore#1:1268
iexplore#2:4336
...