使用 Process PerformanceCounters,我如何知道实例与哪个进程关联?

发布于 2024-09-18 01:57:18 字数 305 浏览 1 评论 0原文

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

例如这段代码:

var cat = new PerformanceCounterCategory("Process");

var names = cat.GetInstanceNames();

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

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

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

When querying instances for the the "Process" performance counter category there might be multiple instances of of a process with the same name.

For example this code:

var cat = new PerformanceCounterCategory("Process");

var names = cat.GetInstanceNames();

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

Might print these results:
...
iexplore
iexplore#1
iexplore#2
iexplore#3
...

How do I know which process each of these counter instances corresponds to?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

獨角戲 2024-09-25 01:57:18

“进程”类别中有一个名为“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);
}

这将打印:

...

i探索:548

iexplore#1:1268

iexplore#2:4336

...

There is a PerformanceCounter named "ID Process" in the "Process" category that will return the pid of the process that the performance counter instance corresponds to.

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);
}

This will print:

...

iexplore: 548

iexplore#1: 1268

iexplore#2: 4336

...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文