Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to get the NextValue of a PerformanceCounter class using PhysicalDisk. Some reason that I can't seem to find out why, it return's 0 every time.

  PerformanceCounter pcDiskTime = new PerformanceCounter("PhysicalDisk", "% Disk Time", "_Total");
  Single sinDisk = pcDiskTime.NextValue(); //returns 0.0

Using the above and calling pcDiskTime.NextValue return's 0. I have other counter's that are working just fine and return what I need.

.RawValue does return something, but is not the value I would need. Is there something obvious that I am not doing?

Note: I have verified through Performance Monitor these are indeed the correct category, counter name and instance name. I have also tried to call .NextValue() twice as sometimes the first return's 0.0, but this does not help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
199 views
Welcome To Ask or Share your Answers For Others

1 Answer

First time it will return zero because there is no previous value to compare, do as below.

PerformanceCounter pcDiskTime = new PerformanceCounter("PhysicalDisk", "% Disk Time", "_Total");
//first time call 
float perfCounterValue = pcDiskTime.NextValue();
//wait some time
System.Threading.Thread.Sleep(1000);
//get the value again
perfCounterValue = pcDiskTime.NextValue();

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...