当前进程的性能计数器的竞争条件

发布于 2024-12-06 17:13:26 字数 729 浏览 0 评论 0 原文

我试图绕过旧的“枚举进程对象实例来获取进程对象的列表然后我可以查询他们的进程 ID 并与我自己的进行比较。

基于此,我可以使用正确的实例索引构建性能计数器路径(以创建类似于 \Process(my_program#3)\ 的内容),然后我可以用它来查询任何计数器但是,如果 my_program 的一个或多个其他实例在 PdhAddCounter 调用?如果我理解正确,这意味着我的计数器路径现在引用了不同的进程或者现在无效。它们甚至可能在查询进程 ID 时消失...

在使用计数器路径获取计数器句柄之前,如何防止计数器路径变得无效?

I am trying to get around the old "How do I get a Windows Performance Counter for the current process" issue. Basically I am enumerating Process Object instances to get a list of Process objects that I can then query for their process id and compare to my own.

Based on this I can build a performance counter path using the correct instance index (to create something similar to \Process(my_program#3)\<counter>) that I can then use to query whatever counter it is that I am interested in. But what happens if one or more of the other instances of my_program exit prior to the PdhAddCounter call? If I understand correctly, this would mean that my counter path now refers to a different process or is now invalid. They might even disappear while querying for the process id...

How do I prevent the counter path from becoming invalid before I can use it to get a counter handle?

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

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

发布评论

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

评论(1

揪着可爱 2024-12-13 17:13:26

哇,你是对的。对我来说,这似乎是一个主要的设计缺陷。基本上,如果实例的名称不唯一,就不可能可靠地监视该实例。我确实偶然发现了一个解决方法对于 Process 和 Thread 对象,但这是可能影响其他应用程序的全局设置。

我认为最安全的方法是监视所有进程对象,每次收集数据时都会检查并找到具有所需进程 ID 的对象。

Wow, you are right. This seems like a major design flaw to me. Basically it is impossible to reliably monitor an instance if it's name is not unique. I did stumble across a workaround specifically for the Process and Thread objects, but that's a global setting that could affect other applications.

I think the safest way to do this would be to watch all process objects, and each time you collect the data go through and find the one with the desired Process Id.

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