简体   繁体   中英

PerformanceCounter always 0

I try to read windows performance counters "LogicalDisk / Disk Reads/sec" and "Cluster CSV File System / IO Reads/sec". I do it so:

string category = "Cluster CSV File System";
string counter = "IO Reads/sec";
string instance = "Volume1";
for (int i = 0; i < 60; i++)
{
    using (PerformanceCounter perfCounter = new PerformanceCounter(category, counter, instance))
    {
        float value = perfCounter.NextValue();
        Console.WriteLine(i + " - {0}({1}): {2}", category, counter, value);
    }
    Thread.Sleep(1000);
}

But NextValue() always return 0. Although values is not 0 in performance monitor graph.

How can I get correct values for this counters?

You should definitely read the documentation

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.

Thus:

using(...)
{
    perfCounter.NextValue(); // discard 0
    Thread.Sleep(1000);
    float value = perfCounter.NextValue();
    Console.WriteLine(i + " - {0}({1}): {2}", category, counter, value);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM