我在使用性能计数器时有点挣扎。有些计数器总是首先得到0,所以我有一个想法,基本上只需要将sleep.thread执行1秒钟,然后再将值赋给字段。
但后来我遇到了性能问题,所以我读了一些提示,我应该这样写:
private PerformanceCounter pcMemory;
private void InitPerfmem()
{
this.pcMemory = new PerformanceCounter();
this.pcMemory.CategoryName = "Memory";
this.pcMemory.CounterName = "Available MBytes";
this.pcMemory.CounterName = "Page Faults/sec";
this.pcMemory.NextValue();
}
private PerformanceCounter pcProcess;
private void InitPerfCPU()
{
this.pcProcess = new PerformanceCounter();
this.pcProcess.CategoryName = "Processor";
this.pcProcess.CounterName = "Interrupts/sec";
this.pcProcess.InstanceName = "_Total";
this.pcProcess.CounterName = "% Processor Time";
this.pcProcess.InstanceName = "_Total";
this.pcProcess.NextValue();
}
private void tmrProcess_Tick(System.Object sender, System.EventArgs e)
{
cpInt.Text = this.pcProcess.NextValue().ToString();
cpPower.Text = this.pcProcess.NextValue().ToString() + "%"; //Display
}
private void tmrMemory_Tick(System.Object sender, System.EventArgs e)
{
pgFaults.Text = this.pcMemory.NextValue().ToString();
avlMem.Text = this.pcMemory.NextValue().ToString();
}
这是否可以避免第一个零结果?我也收到错误“对象引用未设置为对象的实例” 尝试阅读无数文章,但大多数都专注于使用刷新该日期的按钮。我的定时器设置为始终启用并刷新间隔为1秒,以提供更多的实时工具,就像Windows中的性能监视器一样。
任何能够让我朝着正确的方向前进的人都会在这里?