为什么我的性能计数器不会改变?

发布于 2024-08-05 20:29:25 字数 1496 浏览 2 评论 0原文

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

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

计数器类别和计数器已创建并可在性能监视器中查看。

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

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

但是,当我坐下来观察性能监视器中的计数器时,没有任何反应,该值永远不会改变。

那么我做错了什么?

如果我添加对 nextValue() 或 rawValue() 的调用,则该值将按我的预期返回,但 Windows 性能监视器仍然显示一条平线,例如

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

编辑: 我发现如果我关闭性能监视器,然后重新打开它而不删除计数器,突然它意识到有一个新值。因此,这些值正在设置并保留,但性能监视器看不到更改。

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.

So what am I doing wrong?

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

Edit: 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.

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

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

发布评论

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

评论(3

时光与爱终年不遇 2024-08-12 20:29:25

后续行动已有序进行。无论如何,在Win7下,性能监视器似乎可能无法按预期工作。当我编写测试代码时,我在创建计数器后暂停了应用程序,以便启动性能监视器。一旦我让它继续,监视器就不会改变它的计数器,尽管底层计数器被改变了。

如果我随后退出性能监视器并重新启动它,则会显示测试程序中的最后一个计数器值,表明其设置正确。如果我再次运行测试程序,仅更改值,性能监视器最终会发现更改。

因此,正如每个人都指出的那样,代码是正确的,是 Windows 性能监视器出现了异常。

谢谢大家的回答!

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.

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

Thank you all for your answers!

不羁少年 2024-08-12 20:29:25

您是否有可能在测试过程中更改了计数器名称?您的代码不会验证该类别是否包含您的计数器 - 它仅检查该类别是否存在,如果存在,则不会创建该类别。

如果您在首次创建类别后更改了计数器名称,则您的新计数器将不会存在于该类别中,并且在性能监视器中查看计数器名称时,您可能会遗漏计数器名称的细微差别。

Is it possible in the course of your testing that you changed the counter name? Your code doesn't verify that the category contains your counter - it only checks if the category exists, and if it does, it doesn't create the category.

If you've changed counter names since the first creation of the category, your new counter would not exist in the category and you might be missing a slight difference in the name of the counter when looking at it in Performance Monitor.

一页 2024-08-12 20:29:25

你的代码看起来不错。从我的工作示例来看,唯一的区别是我在设置 RawValue 后调用增量方法。

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

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

myCounter.Close();

Your code looks good. From my working example the only difference is that I call the increment method after setting the RawValue.

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

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

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