Problem creating PerformanceCounters

  • Thread starter mark webb, autodesk
  • Start date
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');
}
}
 
M

mark webb, autodesk

Another problem. In the code below, the line pc.RawValue=0; raises an
InvalidOperation Exception with the text
"The operation couldn't be completed, potential internal deadlock."

I cannot see any major differences between this code, and a sample from
CodeProject that works

regards
Mark

public PerformanceCounter AddCounter(string name, string desc,
PerformanceCounterType type)
{
PerformanceCounter pc;
try
{
pc = GetCounter(name);
if (null != pc)
{
if (!m_counters.Contains(pc))
{
m_counters.Add(pc);
}
return pc;
}

// get rid of the category if it already exists...
if (PerformanceCounterCategory.Exists(m_perfObjName))
{
PerformanceCounterCategory.Delete(m_perfObjName);
}

if (!PerformanceCounterCategory.Exists(m_perfObjName))
{
if (null == m_ccdc)
{
m_ccdc = new CounterCreationDataCollection();
}
CounterCreationData ccd = new CounterCreationData();
ccd.CounterName = name;
ccd.CounterHelp = desc;
ccd.CounterType = type;
m_ccdc.Add(ccd);

PerformanceCounterCategory.Create(m_perfObjName, m_perfObjDesc,
m_ccdc);
}

// create the counter and initialize it
pc = new PerformanceCounter();
pc.CategoryName = m_perfObjName;
pc.CounterName = name;
pc.MachineName = ".";
pc.ReadOnly = false;
pc.RawValue = 0;

m_counters.Add(pc);
}
catch (System.InvalidOperationException e)
{
Console.WriteLine(e.Message);
return null;
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
throw;
}
finally
{
}
return pc;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top