使用.net性能计数器时崩溃



我正在尝试在我的应用程序中使用。net性能计数器。下面是代码:

if (!PerformanceCounterCategory.Exists("Processor"))
{
    CounterCreationDataCollection CCDC = new CounterCreationDataCollection();
    // Add the counter.
    CounterCreationData NOI64 = new CounterCreationData();
    NOI64.CounterType = PerformanceCounterType.NumberOfItems64;
    NOI64.CounterName = "%Processor Time";
    CCDC.Add(NOI64);
    // Create the category.
    PerformanceCounterCategory.Create("Processor", "", PerformanceCounterCategoryType.SingleInstance, CCDC);
}
PerformanceCounter PC = new PerformanceCounter("Processor", "%Processor Time", false);
PC.RawValue = 0;

当我执行这段代码时,我在下面提到的PerformanceCounter PC = new PerformanceCounter("Processor", "%Processor Time", false);处崩溃了

类型为"System"的未处理异常。InvalidOperationException"发生在System.dll

附加信息:请求的性能计数器不是自定义计数器,它必须初始化为只读。

我也尝试了using lodctr命令,但它不能工作,如所述的请求的性能计数器不是自定义计数器,它必须初始化为只读

那么你想达到什么目的呢?它清楚地表明,您应该将其初始化为只读。因此,根据文档,您需要传递true而不是false作为第三个参数。

同样,不要给RawValue属性赋零值。这不起作用(因为计数器是只读的)。

最新更新