2016-11-13 1 views
3

Anfahrt mit:Erste InvalidOperationException, wenn Performancecounters

Ausnahme geworfen: 'System.InvalidOperationException' in System.dll

Zusätzliche Informationen:

Kategorie existiert nicht.

Der Code:

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

Erste Fehler auf jeder .nextValue();

Ich habe versucht, das Hinzufügen Processor Information in CategoryName, aber hilft auch nicht.

EDIT: @ Jim dies ist mein Code nach Ihren Änderungen:

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

Könnten Sie zumindest geben Sie die Stack-Trace und erklären ein wenig, was du bist versuchen zu tun ? – Fourat

Antwort

2

Sie schaffen neue Leistungsindikatoren jedes Mal, wenn der Timer tickt, müssen Sie nur auf die Zähler einmal initialisieren.

Form1-Code Hinter

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

Randbemerkung:

Auch Dispose die Zähler, wenn sie nicht mehr verwendet wird. Vielleicht beim Abschluss des Formulars.

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

Ergebnis

enter image description here


Fall eines Fehler

Wenn mein Beispiel noch einen Fehler wirft, dann ist dies wahrscheinlich, weil ein oder mehr Leistungsindikatoren auf Ihr System ist beschädigt.Erfolgreich:

Rebuild alle Leistungsindikatoren:

  • Öffnen Sie die Eingabeaufforderung (mit Administratorrechten)
  • Run-Befehl: lodctr /R

Die Botschaft:

Info Einstellung des Leistungsindikators ...

wird bei Erfolg angezeigt.

Im Fall erhalten Sie die Meldung:

Unable Leistungsindikator Einstellung von System-Backup-Speicher, Fehlercode 2

Mögliche Lösungen für den Wiederaufbau:

  • Schließen Sie alle laufenden Programme
  • Stellen Sie sicher, dass Sie lodctr /R mit Großbuchstabenverwenden
  • Wenn im Verzeichnis system32 Umzug in das Verzeichnis SysWOW64 (mit cd ..> cd syswow64) und den lodctr /R Befehl in diesem Verzeichnis wiederholen
+0

Ich habe versucht, was Sie sagten, und jetzt bekomme ich eine .NullReferenceException und eine "Objektreferenz nicht auf eine Instanz eines Objekts festgelegt". – Commongrate

+0

@Comongrate das Beispiel, das ich gemacht habe, wird aus der Box laufen. Versuchen Sie, ein neues winforms-Projekt zu erstellen und fügen Sie den Code genau wie meinen zum Testen hinzu. (Nicht foget, um 'InitializeCounters(); 'aufzurufen, um die Zähler zu initialisieren. Die Methode wird in meinem Beispiel im Form-Konstruktor aufgerufen. – Jim

+0

Ich habe deinen Code kopiert, alles hinzugefügt und ausgeführt, auf dem ramCounter = new PerformanceCounter (" "Memory", "Available MBytes"); Zeile Ich habe den gleichen "cat does not exist" -Fehler, also habe ich das kommentiert und ausgeführt, es lief, aber wenn ich auf die Schaltfläche 1 klicke, löst es kein Ereignis und die Textbox aus ist leer – Commongrate

0

eine Kategorie erstellen, bevor Performance verwenden. Weitere Details und Beispiele in 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); 
    } 
Verwandte Themen