Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用性能计数器,效果良好_C#_Winforms - Fatal编程技术网

C# 使用性能计数器,效果良好

C# 使用性能计数器,效果良好,c#,winforms,C#,Winforms,我在使用performancecounters时遇到了一些困难。有些计数器总是先得到0,所以我想到了一个主意,在将值分配给字段之前,基本上只需执行sleep.thread 1秒 但是后来我在性能方面遇到了问题,所以我读了一些技巧,我应该这样写: private PerformanceCounter pcMemory; private void InitPerfmem() { this.pcMemory = new PerformanceCounter();

我在使用performancecounters时遇到了一些困难。有些计数器总是先得到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中的性能监视器一样


有人能把我推到正确的方向吗?

你是在代码中的任何地方调用
InitPerfmem()
还是
initperfcu()
?在你的例子中我看不出来。。。不初始化
性能计数器
对象肯定会导致
对象引用
错误。如果调用了这些方法,那么哪一行会抛出错误?第一个结果为零是什么意思?第一次访问
NextValue
?请澄清。第一次打电话给NextValue