簡體   English   中英

使用Process PerformanceCounters,我如何知道實例與哪個流程相關聯?

[英]With Process PerformanceCounters how do I know what process an instance is associated with?

當為“流程”性能計數器類別查詢實例時,可能存在多個同名流程的實例。

例如這段代碼:

var cat = new PerformanceCounterCategory("Process");

var names = cat.GetInstanceNames();

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

可能會打印以下結果:... iexplore iexplore#1 iexplore#2 iexplore#3 ...

我如何知道這些計數器實例分別對應哪個進程?

在“進程”類別中有一個名為“ ID進程”的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

...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM