使用 .net 性能计数器时崩溃
Crash while using .net performance counters
我正在尝试在我的应用程序中使用 .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);
处崩溃
An unhandled exception of type 'System.InvalidOperationException'
occurred in System.dll
Additional information: The requested Performance Counter is not a custom counter, it has to be initialized as ReadOnly.
我也尝试了 using lodctr
命令,但它确实像 The requested Performance Counter is not a custom counter, it has to be initialized as ReadOnly
中提到的那样工作
那么你想达到什么目的?它明确表示,您应该将其初始化为只读。所以,根据 documentation 你需要传递 true
而不是 false
作为第三个参数。
此外,不要将零分配给 RawValue
属性。这是行不通的(因为计数器是只读的)。
我正在尝试在我的应用程序中使用 .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);
处崩溃
An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll
Additional information: The requested Performance Counter is not a custom counter, it has to be initialized as ReadOnly.
我也尝试了 using lodctr
命令,但它确实像 The requested Performance Counter is not a custom counter, it has to be initialized as ReadOnly
那么你想达到什么目的?它明确表示,您应该将其初始化为只读。所以,根据 documentation 你需要传递 true
而不是 false
作为第三个参数。
此外,不要将零分配给 RawValue
属性。这是行不通的(因为计数器是只读的)。