首页 > 解决方案 > 为什么系统正常运行时间的性能计数器需要 2 次调用 NextValue?

问题描述

这是您获取系统正常运行时间的性能计数器的方式:

public TimeSpan GetSystemUptime(){
   PerformanceCounter upTime = new PerformanceCounter("System", "System Up Time");
   upTime.NextValue();
   return TimeSpan.FromSeconds(upTime.NextValue());
}

NextValue 必须被调用两次,因为它在第一次调用时为 0。但我不明白为什么它使用必须读取两次的计数器。我知道像 CPU Usage [new PerformanceCounter("Processor Information", "% Processor Time", "_Total")] 这样的东西需要 2 个值,因为它计算的是一段时间内的平均值。但是为什么在计算系统正常运行时间时需要测量 2 次?您是否只需要测量当前时间并将其与启动时间进行比较?

标签: performancecounterperfmon

解决方案


推荐阅读