性能计数器:未识别的不良行为

发布于 2024-11-29 03:06:54 字数 1465 浏览 1 评论 0原文

我有一个计数器检查应用程序,它监视 .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);

但不在“Init”中的“value = this.pc.NetxtValue”[标记的位置]

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

任何帮助都会很棒!

到目前为止谢谢 最好的问候,

++mabra

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();
        }
        catch(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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野稚 2024-12-06 03:06:54

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文