c# - Getting InvalidOperationException when using PerformanceCounters -
getting:
exception thrown: 'system.invalidoperationexception' in system.dll
additional information:
category not exist.
the 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(); } } }
getting errors on every .nextvalue();
i have tried adding processor information
in categoryname
, doesn't either.
edit: @jim code after making changes :
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(); // 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(); } }
}
you creating new performance counters every time timer ticks, need initialize counters once.
form1 code behind
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(); // 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(); }
side note:
also dispose
counters when not using them anymore. maybe on form closing event.
cpucounter.dispose(); ramcounter.dispose();
result
case of error
if example still throws error, because 1 or more performance counters on system corrupted.
rebuild performance counters:
- open command prompt (with admin privileges)
- run command:
lodctr /r
the message:
info: rebuilt performance counter setting ...
will appear on success.
in case receive message:
unable rebuild performance counter setting system backup store, error code 2
possible solutions:
- close running programs
- make sure use
lodctr /r
uppercaser
- if in directory system32 move directory syswow64 (with cd.. > cd syswow64) , retry
lodctr /r
command in directory
Comments
Post a Comment