开发者

PerformanceCounters:Unidentified bad behavior

I have a counter check application, which monitors PerformanceCounters [PC] from .Net.

I am using a wrapper-class for the PC to allow synchronized access etc. The CounterWrapper has a Init method, where the following code is executed:

PerformanceCounterCategory pcc = new PerformanceCounterCategory(this.categoryName, this.machineName);
if(pcc.CounterExists(this.counterName))
{
    if(this.pc != null)    //Could exist from earlier use.
    {
        try
        {
            this.pc.Close();
            this.pc.Dispose();
        }
        catc开发者_如何转开发h(Exception ex)
        {
            ...log
        }
    }

    this.pc = new PerformanceCounter(this.categoryName, this.counterName, this.instanceName, this.machineName);
    float value = pc.NextValue();        //Initial read//////-----MARK-----.
}

This code works initial well.

After one of the watched computers goes offline - this is registered inside the Update method - the code above does no longer work and to test the remote availability of that counter, the above "Init" method is called again. Sometimes this works, sometimes this fails. If it fails, I see:

InvalidOperation;Message: Cannot access a closed registry key. Object name: 'HKEY_PERFORMANCE_DATA'., source: mscorlib

This is at the MARKed position in the code above.

I have expected the code to fail here:

PerformanceCounterCategory pcc = new PerformanceCounterCategory(this.categoryName, this.machineName);
if(pcc.CounterExists(this.counterName))

or here:

this.pc = new PerformanceCounter(this.categoryName, this.counterName, this.instanceName, this.machineName);

But not in "Init" at "value = this.pc.NetxtValue" [the MARKed pos]

There seems to be somethings strange in the framework [and this is not the first time, I see something strange with it at PerformanceCounters].

Any help would be great!

Thanks so far and best regards,

++mabra


You close and dispose the PC member, but you don't set it to null, and that's what you test against the next time you call in, so you're probably calling Close on an already closed instance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜