c# – 使用PerformanceCounters时出现InvalidOperationException

获得:

Exception thrown: ‘System.InvalidOperationException’ in System.dll

附加信息:

Category does not exist.

代码 :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
    PerformanceCounter cpuCounter;
    PerformanceCounter ramCounter;
    public Form1()
    {
        InitializeComponent();
    }
    int timeX = 0;
    private void timer1_Tick(object sender, EventArgs e)
    {
        cpuCounter = new PerformanceCounter();
        cpuCounter.CategoryName = "Processor";
        cpuCounter.CounterName = "% Processor Time";
        cpuCounter.InstanceName = "_Total";


        float cpuUsage = 0.00F;
        cpuCounter.NextValue();
        cpuUsage = cpuCounter.NextValue();
        textBox1.Text = cpuUsage.ToString();


        ramCounter = new PerformanceCounter("Memory", "Available MBytes");
        float ram = ramCounter.NextValue();
        textBox2.Text = ram.ToString();

        chart1.Series["CPU Usage"].Points.AddXY(timeX, (int)cpuUsage);
        chart2.Series["Memory Use"].Points.AddXY(timeX, (int)ram);

    }

    private void button1_Click(object sender, EventArgs e)
    {
       timer1.Start();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        timer1.Stop();
    }
}
}

在每个.nextValue()上获取错误;

我尝试在CategoryName中添加处理器信息,但也没有帮助.

编辑:
@Jim这是我做出更改后的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
namespace WindowsFormsApplication12
{
public partial class Form1 : Form
{
    PerformanceCounter cpuCounter;
    PerformanceCounter ramCounter;

    public Form1()
    {
        InitializeComponent();

        InitializeCounters();
    }

    private void InitializeCounters()
    {
        cpuCounter = new PerformanceCounter();
        cpuCounter.CategoryName = "Processor";
        cpuCounter.CounterName = "% Processor Time";
        cpuCounter.InstanceName = "_Total";

       // ramCounter = new PerformanceCounter("Memory", "Available MBytes");
    }

    int timeX = 0;
    private void timer1_Tick(object sender, EventArgs e)
    {
        float cpuUsage = 0.00F;
        cpuUsage = cpuCounter.NextValue();
        textBox1.Text = cpuUsage.ToString();

        float ram = ramCounter.NextValue();
        textBox2.Text = ram.ToString();

        // Your chart stuff
        //chart1.Series["CPU Usage"].Points.AddXY(timeX, (int)cpuUsage);
        //chart2.Series["Memory Use"].Points.AddXY(timeX, (int)ram);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        timer1.Start();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        timer1.Stop();
    }
}

}

最佳答案 每次计时器滴答时,您都在创建新的性能计数器,您只需要初始化计数器一次.

Form1代码背后

PerformanceCounter cpuCounter;   
PerformanceCounter ramCounter;

public Form1()
{
    InitializeComponent();

    InitializeCounters();
}

private void InitializeCounters()
{
    cpuCounter = new PerformanceCounter();
    cpuCounter.CategoryName = "Processor";
    cpuCounter.CounterName = "% Processor Time";
    cpuCounter.InstanceName = "_Total";

    ramCounter = new PerformanceCounter("Memory", "Available MBytes");
}

int timeX = 0;
private void timer1_Tick(object sender, EventArgs e)
{        
    float cpuUsage = 0.00F;
    cpuUsage = cpuCounter.NextValue();
    textBox1.Text = cpuUsage.ToString();

    float ram = ramCounter.NextValue();
    textBox2.Text = ram.ToString();

    // Your chart stuff
    //chart1.Series["CPU Usage"].Points.AddXY(timeX, (int)cpuUsage);
    //chart2.Series["Memory Use"].Points.AddXY(timeX, (int)ram);
}

private void button1_Click(object sender, EventArgs e)
{
    timer1.Start();
}

private void button2_Click(object sender, EventArgs e)
{
    timer1.Stop();
}

边注:

不再使用时也要处理计数器.也许在Form Closing事件上.

cpuCounter.Dispose();
ramCounter.Dispose();

结果

《c# – 使用PerformanceCounters时出现InvalidOperationException》

出错的情况

如果我的示例仍然抛出错误,这很可能是因为系统上的一个或多个性能计数器已损坏.

重建所有性能计数器:

>打开命令提示符(具有Admin权限)
>运行命令:lodctr / R.

消息:

Info: Successfully rebuilt performance counter setting …

将在成功时出现.

如果您收到消息:

Unable to rebuild performance counter setting from system backup store, error code is 2

可能的解决方案:

>关闭所有正在运行的程序
>确保使用大写R的lodctr / R.
>如果在目录system32中移动到目录SysWOW64(使用cd ..> cd syswow64)并在此目录中重试lodctr / R命令

点赞