簡體   English   中英

使用.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);崩潰PerformanceCounter PC = new PerformanceCounter("Processor", "%Processor Time", false); 如以下所說的

System.dll中發生了類型為'System.InvalidOperationException'的未處理異常

附加信息:所請求的性能計數器不是自定義計數器,必須將其初始化為ReadOnly。

我也嘗試過using lodctr命令,但它確實如要求的那樣工作:請求的性能計數器不是自定義計數器,必須將其初始化為ReadOnly

那你想達到什么目的呢? 它清楚地表明,您應該將其初始化為只讀。 因此,根據文檔,您需要傳遞true和not false作為第三個參數。

另外,不要為RawValue屬性分配零。 這是行不通的(因為計數器是只讀的)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM