为什么我的绩效指标不会改变? [英] Why won't my performance counters change?

查看:93
本文介绍了为什么我的绩效指标不会改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里一定做错了什么.我创建一个自定义性能计数器,如下所示:

I must be doing something very wrong here. I create a custom performance counter as follows:

string counterCategory = "Test Category";
string counterName = "Test Counter";

if (!PerformanceCounterCategory.Exists(counterCategory))
{
    Console.WriteLine("Creating Counters");

    CounterCreationDataCollection counterCreationDataCollection =
        new CounterCreationDataCollection();

    counterCreationDataCollection.Add(
        new CounterCreationData(counterName,
        "Description",
        PerformanceCounterType.NumberOfItems32)
    );

    PerformanceCounterCategory.Create(counterCategory,
        "My category description/Help",
        PerformanceCounterCategoryType.SingleInstance,
        counterCreationDataCollection);
}

计数器类别和计数器已创建,并且可以在性能监视器中查看.

The counter category and counter are created and viewable in performance monitor.

然后我尝试更改计数器的值

I then attempt to change the value of the counter

PerformanceCounter myCounter = 
    new PerformanceCounter(counterCategory, counterName, false);

for (int i = 0; i < 10; i++)
{
    Console.WriteLine("Setting to "+i);
    myCounter.RawValue = i;
    Thread.Sleep(200);
}

myCounter.Close();

但是,当我坐在性能监视器中观察计数器时,什么也没发生,值永远不会改变.

However as I sit and watch the counter in performance monitor nothing happens, the value never changes.

那我在做什么错了?

如果我添加对nextValue()或rawValue()的调用,则返回的值将按预期返回,但Windows Performance Monitor仍显示平线,例如

If I add a call to nextValue(), or rawValue() the value from that is returned as I expected but the Windows Performance Monitor still shows a flat line, e.g.

for (int i = 0; i < 10; i++)
{
    Console.WriteLine("Setting to "+i);
    myCounter.IncrementValue()
    Console.WriteLine("Next Value = "+myCounter.RawValue()); 
    Thread.Sleep(200);
}

我发现,如果关闭性能监视器,然后在不删除计数器的情况下将其重新打开,则突然意识到存在新的价值.因此,这些值正在设置并保持不变,但是Performance Monitor看不到更改.

I've found that if I close the performance monitor and then reopen it without deleting the counters, that suddenly it realises there's a new value. So the values are being set, and persisting, however Performance Monitor doesn't see the changes.

推荐答案

请按计划进行跟进.无论如何,在Win7下,性能监视器似乎可能无法正常工作.当我编写测试代码时,我在创建计数器后暂停了应用程序,以启动性能监视器.一旦让它继续运行,即使基础计数器已更改,监视器也不会更改它的计数器.

A follow up is in order. It appears, under Win7 anyway, that the performance monitor may not work as expected. When I wrote the test code I paused the application after creating the counters, in order to start the performance monitor. Once I let it continue the monitor never changed it's counters, despite the underlying counter being changed.

如果我随后退出性能监视器,然后重新启动它,则会显示测试程序中的最后一个计数器值,表明它已正确设置.如果我然后再次运行测试程序,只是更改值,性能监视器将最终接受更改.

If I then quit the performance monitor and restarted it the last counter value in the test program would be shown, indicating that it was being set correctly. If I then ran the test program again, just changing values, performance monitor would finally pick up the changes.

因此,正如每个人所指出的那样,该代码是正确的,它是Windows Performance监视器的行为不正常.

So the code, as everyone indicated is correct, it was the Windows Performance monitor that was misbehaving.

谢谢大家的回答!

这篇关于为什么我的绩效指标不会改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆