M
mark webb, autodesk
I am trying to create new performance counters using the .Net framework, and
even simple examples fail with exceptions.
For example, the following code raises an InvalidOperationException while
calling CounterExists. The message indicates that the category does not
exist (even though the category must exist for the code to have gotten this
far)
Any thoughts on the problem? I tried with both the 1.0 and 1.1 versions of
the framework.
regards
Mark
using System;
using System.Diagnostics;
using System.Threading;
public class PCWrite
{
public static void Main(string[] args)
{
string objectName = "MyObj";
string counterName = "MyCounter";
string instanceName = "MyInstance";
if(!PerformanceCounterCategory.Exists(objectName))
{
PerformanceCounterCategory.Create(objectName,"Simple Counter
Object",counterName,"Simple Counter");
}
Console.WriteLine("Category Exists");
if(!PerformanceCounterCategory.CounterExists(objectName, counterName))
{
Console.WriteLine("The counter does not exists!");
return;
}
Console.WriteLine("Counter Exists");
PerformanceCounter aCounter = new PerformanceCounter(objectName,
counterName ,instanceName, false);
aCounter.RawValue = 50;
Console.WriteLine("Press \'q\' to quit the sample");
Console.WriteLine("Press \'+\' to increment the counter");
Console.WriteLine("Press \'-\' to decrement the counter");
Console.WriteLine("Started");
int command;
do
{
command = Console.Read();
if(command=='-') aCounter.IncrementBy(-5);
if(command=='+') aCounter.IncrementBy(5);
}
while(command!='q');
}
}
even simple examples fail with exceptions.
For example, the following code raises an InvalidOperationException while
calling CounterExists. The message indicates that the category does not
exist (even though the category must exist for the code to have gotten this
far)
Any thoughts on the problem? I tried with both the 1.0 and 1.1 versions of
the framework.
regards
Mark
using System;
using System.Diagnostics;
using System.Threading;
public class PCWrite
{
public static void Main(string[] args)
{
string objectName = "MyObj";
string counterName = "MyCounter";
string instanceName = "MyInstance";
if(!PerformanceCounterCategory.Exists(objectName))
{
PerformanceCounterCategory.Create(objectName,"Simple Counter
Object",counterName,"Simple Counter");
}
Console.WriteLine("Category Exists");
if(!PerformanceCounterCategory.CounterExists(objectName, counterName))
{
Console.WriteLine("The counter does not exists!");
return;
}
Console.WriteLine("Counter Exists");
PerformanceCounter aCounter = new PerformanceCounter(objectName,
counterName ,instanceName, false);
aCounter.RawValue = 50;
Console.WriteLine("Press \'q\' to quit the sample");
Console.WriteLine("Press \'+\' to increment the counter");
Console.WriteLine("Press \'-\' to decrement the counter");
Console.WriteLine("Started");
int command;
do
{
command = Console.Read();
if(command=='-') aCounter.IncrementBy(-5);
if(command=='+') aCounter.IncrementBy(5);
}
while(command!='q');
}
}