NextValue()返回值0 [英] NextValue() return's 0

查看:215
本文介绍了NextValue()返回值0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PhysicalDisk获取PerformanceCounter类的NextValue.我似乎无法找出原因的某些原因,它每次都返回0.

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

使用以上内容并调用pcDiskTime.NextValue返回return 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确实返回了一些内容,但不是我需要的值.有没有明显的我没有做的事情?

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

注意:我已经通过Performance Monitor验证了这些确实是正确的类别,计数器名称和实例名称.我还尝试两次调用.NextValue(),有时是第一次返回的0.0,但这无济于事.

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.

推荐答案

由于没有可比较的先前值,第一次它将返回零,请执行以下操作.

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

如果计数器的计算值取决于两次计数器读取,则 第一次读取操作将返回0.0.重置性能计数器 指定不同计数器的属性等效于创建一个 新的性能计数器,以及使用新的第一次读取操作 属性返回0.0.两次通话之间的建议延迟时间 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.

这篇关于NextValue()返回值0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆