views:

940

answers:

1

I am trying to c# PerformanceCounter Library part of System.Diagnostic. While setting the raw value of the counter using

public long RawValue { set; get; }

I was passing the Rawalue to 0. However I noticed that Maximum value of the counter was reset to a very large number. Previous value of the counter was 2

Can someone help me out and point out any mistake I might be making, here is my code

using (PerformanceCounter ctr = new     
PerformanceCounter(Settings.Instance.SetSourceAppliacationName, counter.ToString(), false))
{
    if (incrementCounter)
    {
        ctr.IncrementBy(value);
    }
    else
    {
        ctr.RawValue = value;
    }
}
A: 

I don't think you're really making a mistake.

The Maximum value is not a feature of the PerformanceCounter itself; it is a part of the monitoring tool (like PerfMon). You can't set it using the PerformanceCounter class.

One thing you might want to do is to set the RawValue to zero before your application starts trying to apply useful data into it. This can be tricky if you have multiple applications using either a single instance category (PerformanceCounterCategoryType.SingleInstance), or the same instance name (like a "total" instance name) for PerformanceCounterCategoryType.MultiInstance.

The very high number is likely just some random number from an uninitialized block of memory that is used to store the performance counter variable. Because multiple applications might try to access an ongoing performance counter by instantiating a new PerformanceCounter object, that instantiation process does not automatically set the value to zero, by design.

Alan McBee
I'd be surprised if the entire development team for PerfMon didn't feel it was necessary to initialize the variable of primary user interest to zero.
gWiz