我正在尝试在我的应用程序中使用.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
中附加信息:请求的性能计数器不是自定义计数器,必须初始化为ReadOnly。
我也尝试了using lodctr
命令,但它的工作方式与The requested Performance Counter is not a custom counter, it has to be initialized as ReadOnly
答案 0 :(得分:1)
那你想要实现什么目标?它清楚地说,你应该把它初始化为只读。因此,根据documentation,您需要传递true
而不是false
作为第三个参数。
另外,不要将RawValue
属性赋予零。这不起作用(因为计数器是只读的)。