PerformanceCounters:未识别的不良行为

本文关键字:不良 识别 PerformanceCounters | 更新日期: 2023-09-27 18:05:15

我有一个计数器检查应用程序,它从。net监视PerformanceCounters [PC]。

我正在使用PC的包装类来允许同步访问等。CounterWrapper有一个Init方法,其中执行以下代码:

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();
        }
        catch(Exception ex)
        {
            ...log
        }
    }
    this.pc = new PerformanceCounter(this.categoryName, this.counterName, this.instanceName, this.machineName);
    float value = pc.NextValue();        //Initial read//////-----MARK-----.
}

这段代码最初运行良好。

当其中一台被监视的计算机脱机后(这在Update方法中注册)上面的代码不再工作,要测试该计数器的远程可用性,请执行上面的"Init"方法再次被调用。有时有效,有时失败。如果失败,我看到:

InvalidOperation;消息:无法访问已关闭的注册表项。对象名称:'HKEY_PERFORMANCE_DATA'。,来源:mscorlib

这是在上面代码中标记的位置。

我已经预料到这里的代码会失败:

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

或此处:

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

但是在"value = this.pc "处没有。NetxtValue"[已标记的pos]

框架中似乎有一些奇怪的东西[这不是第一次,我在PerformanceCounters上看到了一些奇怪的东西]。

任何帮助将是伟大的!

感谢到目前为止和最好的祝福,

+ + mabra

PerformanceCounters:未识别的不良行为

您关闭并处置PC成员,但您不将其设置为null,这是您下次调用时测试的内容,因此您可能在已经关闭的实例上调用close。