gpt4 book ai didi

c# - ASP.NET 性能计数器总是返回 0

转载 作者:太空狗 更新时间:2023-10-29 20:36:39 26 4
gpt4 key购买 nike

我想读取“ASP.NET”性能计数器类别的性能 NextValue()。然而,该类别的计数器始终显示 0,而其他计数器按预期工作。

远程计算机上 perfmon.exe 中的“ASP.NET”计数器工作正常。

我本地机器上针对远程机器的 perfmon.exe 中的“ASP.NET”计数器也显示为 0。

var pc = new PerformanceCounter("ASP.NET", "Requests Current", "", "myRemoteMachine");
pc.NextValue(); // returns always 0
pc.NextValue(); // returns always 0

有什么想法吗?权限或某种防火墙问题?

最佳答案

解决方案是在调用 NextValue 之间休眠 1 秒。

在 VB 中:

Dim cpu As New PerformanceCounter("Processor", "% Processor Time", "_Total", "servername")

cpu.NextValue()

System.Threading.Thread.Sleep(1000)

MyValue = cpu.NextValue()

很难知道它是否仍然返回正确的数字,但它与 perfmon 显示的数字非常接近(在 1 点以内)。我也尝试了 2 秒,它似乎更接近 perfmon 显示的结果。

来自 http://blogs.msdn.com/b/dotnetinterop/archive/2007/02/02/system-diagnostics-performancecounter-and-processor-time-on-multi-core-or-multi-cpu.aspx :

Keep in mind that you need to delay "about 1 second" between calls to NextValue(), as per the documentation!

...以及指向 https://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.nextvalue.aspx 的链接,其中指出:

If the calculated value of a counter depends on two counter reads, the first read operation returns 0.0. Resetting the performance counter properties to specify a different counter is equivalent to creating a new performance counter, and the first read operation using the new properties returns 0.0. The recommended delay time between calls to the NextValue method is one second, to allow the counter to perform the next incremental read.

关于c# - ASP.NET 性能计数器总是返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19543479/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com