2016-11-13 61 views
3

獲得獲得InvalidOperationException異常:在System.dll中使用時PerformanceCounters

'System.InvalidOperationException' 的附加信息:

拋出異常

類別不存在。

代碼:每.nextValue();

我曾嘗試在CategoryName加入Processor Information,但也沒有幫助

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

得到錯誤。

編輯: @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(); 
    } 
} 

}

+1

你能至少給我們堆棧跟蹤和解釋一下什麼是你試圖做什麼? – Fourat

回答

2

您正在創建新的性能計數器每次計時器滴答的時間,你只需要初始化計數器一次

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

旁註:

而且Dispose計數器時不使用它們了。也許在Form Closing事件上。

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

結果

enter image description here


出錯的情況下

如果我的例子仍然拋出一個錯誤,這是最有可能是因爲一個或多個性能計數器上您的系統已損壞。

重建所有性能計數器:

  • 打開命令提示(具有管理員權限)
  • 運行命令:lodctr /R

消息:

信息:成功地重建性能計數器設置...

將出現成功。

如果您收到消息:

無法重建從系統備份存儲性能計數器設置,錯誤代碼是2級

可能的解決方案:

  • 關閉所有正在運行程序
  • 請確保您使用lodctr /R且大寫R
  • 如果在SYSTEM32目錄移動到目錄SysWOW64中(與CD ..> CD SysWow64資料),並在該目錄中重試lodctr /R命令
+0

我試着你說的,現在我得到一個.NullReferenceException和「對象引用未設置爲對象的實例」。 – Commongrate

+0

@說出我所做的例子將會用完。嘗試做一個新的winforms項目,並添加完全像我的測試代碼。 (不要急於調用'InitializeCounters();'來初始化計數器。在我的示例中,在窗體構造函數中調用該方法 – Jim

+0

我複製了你的代碼,添加了所有內容並運行它,在ramCounter = new PerformanceCounter(「內存「,」可用MBytes「);線我得到了同樣的」貓不存在「的錯誤,所以我評論說,並運行它,它跑了,但是當我點擊按鈕1它不會觸發事件和文本框是空的 – Commongrate

0

使用的PerformanceCounter之前創建一個類別。更多細節和例子msdn

const String categoryName = "Processor"; 
    const String counterName = "% Processor Time"; 

    if (!PerformanceCounterCategory.Exists(categoryName)) 
    { 

     CounterCreationDataCollection CCDC = new CounterCreationDataCollection(); 

     // Add the counter. 
     CounterCreationData ETimeData = new CounterCreationData(); 
     ETimeData.CounterType = PerformanceCounterType.ElapsedTime; 
     ETimeData.CounterName = counterName; 
     CCDC.Add(ETimeData);  

     // Create the category. 
     PerformanceCounterCategory.Create(categoryName, 
       "Demonstrates ElapsedTime performance counter usage.", 
      PerformanceCounterCategoryType.SingleInstance, CCDC); 
    }