2009-10-18 54 views
0

我試圖顯示自上次表現反覆發生的操作次數。我已創建使用下面的性能計數器:需要例如使用CounterDelta32的PerformanceCounter

var clearStateCounterData = new CounterCreationData() 
{ 
    CounterName = ClearStateName, 
    CounterHelp = "The number of times the service state has been cleared since the last performance iteration", 
    CounterType = PerformanceCounterType.CounterDelta32 
}; 

然後我叫counter.Increment()在我的應用程序,但我從來沒有看到性能計數器值的舉動。即使我每秒運行多次。

是不是有什麼特別的,我需要或一個特定的值,我需要通過增量來獲得的PerformanceCounter展示些什麼呢?

想通了

我把下面的答案使用此計的例子。謝謝你們的幫助。

回答

1

這裏是爲我工作的例子。

class Program 
{ 
    const string CategoryName = "____Test Category"; 
    const string CounterName = "Clear State Operations"; 

    static void Main(string[] args) 
    { 
     if (PerformanceCounterCategory.Exists(CategoryName)) 
      PerformanceCounterCategory.Delete(CategoryName); 

     var counterDataCollection = new CounterCreationDataCollection(); 

     var clearStateCounterData = new CounterCreationData() 
     { 
      CounterName = CounterName, 
      CounterHelp = "The number of times the service state has been cleared since the last performance iteration", 
      CounterType = PerformanceCounterType.CounterDelta32 
     }; 
     counterDataCollection.Add(clearStateCounterData); 

     PerformanceCounterCategory.Create(CategoryName, "Test Perf Counters", PerformanceCounterCategoryType.SingleInstance, counterDataCollection); 

     var counter = new PerformanceCounter(CategoryName, CounterName, false); 

     for (int i = 0; i < 5000; i++) 
     { 
      var sw = Stopwatch.StartNew(); 
      Thread.Sleep(10300); 
      sw.Stop(); 

      counter.Increment(); 
     } 

     Console.Read(); 
    } 
} 
0

這還不足以創建計數器...根據文檔,您需要創建一個PerformanceCounterCategory並創建一個PerformanceCounter的實例。查看MSDN中的示例:http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.aspx

+0

呀,我做過的一切。櫃檯出現在我的類別中。我似乎無法通過增加計數器來顯示任何值。我猜這是counter.Increment不是正確的事情,或我需要在這種情況下調用的唯一的事情,但我沒有看到這種計數器類型可用的任何示例。 – 2009-10-18 23:17:06

0

創建計數器(使用CounterCreationData和PerformanceCounterCategory中的Create),然後創建計數器實例(使用PerformanceCounter)後,您需要初始化計數器值以獲取該實例在Performance Monitor中啓動。

此外,還要確保你(通過傳遞假以只讀參數)創造了讀寫模式下的計數器。

你可以嘗試設置RawValue = RawValue,或RawValue = 0啓動它,看看它出現。