2012-07-23 123 views
1

我有一個簡單的監視應用程序從PerfMon計數器獲取一些值。即使在本地計算機上進行測試,創建一個新的PerformanceCounter對象也需要30秒多的時間。創建一個新的System.Diagnostics.PerformanceCounter非常慢

using System; 
using System.Diagnostics; 

namespace test_slow_perfmon 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Stopwatch w = new Stopwatch(); 

      w.Start(); 
      PerformanceCounter c = new PerformanceCounter("PhysicalDisk", "Avg. Disk Read Queue Length", "_Total", "localhost"); 
      w.Stop(); 
      Console.WriteLine(string.Format("Creating a counter took {0}ms", w.Elapsed.TotalMilliseconds)); 
     } 
    } 
} 

從那輸出表示超過32s來創建每個計數器。

我能做些什麼(如果有的話)來加速創建計數器?

+0

32秒??? 當您嘗試「。」時速度會更快嗎?作爲「本地主機」的機器名稱instad? – 2012-07-23 12:18:45

回答

3

30秒對我來說聽起來像一個超時,可以指出,這可能是某種網絡問題。

嘗試使用the constructor that doesn't specify a hostname創建性能監視器計數器,看看是否有幫助:

PerformanceCounter c = new PerformanceCounter("PhysicalDisk", "Avg. Disk Read Queue Length", "_Total"); 
+1

這似乎是它。如果我沒有指定主機名,或使用「。」對於本地主機,它的作品發現。這只是「本地主機」,似乎沒有正確解決。 – Cylindric 2012-07-23 12:44:51