c# performanceCounter setting raw value of counter

╄→尐↘猪︶ㄣ 提交于 2019-12-08 05:15:42

问题


I am trying to c# PerformanceCounter Library part of System.Diagnostic. While setting the raw value of the counter using

public long RawValue { set; get; }

I was passing the Rawalue to 0. However I noticed that Maximum value of the counter was reset to a very large number. Previous value of the counter was 2

Can someone help me out and point out any mistake I might be making, here is my code

using (PerformanceCounter ctr = new     
PerformanceCounter(Settings.Instance.SetSourceAppliacationName, counter.ToString(), false))
{
    if (incrementCounter)
    {
        ctr.IncrementBy(value);
    }
    else
    {
        ctr.RawValue = value;
    }
}

回答1:


I don't think you're really making a mistake.

The Maximum value is not a feature of the PerformanceCounter itself; it is a part of the monitoring tool (like PerfMon). You can't set it using the PerformanceCounter class.

One thing you might want to do is to set the RawValue to zero before your application starts trying to apply useful data into it. This can be tricky if you have multiple applications using either a single instance category (PerformanceCounterCategoryType.SingleInstance), or the same instance name (like a "total" instance name) for PerformanceCounterCategoryType.MultiInstance.

The very high number is likely just some random number from an uninitialized block of memory that is used to store the performance counter variable. Because multiple applications might try to access an ongoing performance counter by instantiating a new PerformanceCounter object, that instantiation process does not automatically set the value to zero, by design.



来源:https://stackoverflow.com/questions/613262/c-sharp-performancecounter-setting-raw-value-of-counter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!