2013-07-20 40 views
0

請參見下面的代碼,請:獲得零從性能計數器

PerformanceCounter total_cpu = new PerformanceCounter("Process", "% Processor Time", "_Total"); 
    return total_cpu.NextValue(); 
    //returns 0 

返回0

現在,請參閱本之一:

PerformanceCounter total_cpu = new PerformanceCounter("Process", "% Processor Time", "_Total"); 
    while (true) 
    { 
     float t = total_cpu.NextValue(); 
     System.Threading.Thread.Sleep(1000); 
     return t; 
     //returns correct value (matched with Task Manager) 
    } 

我想要得到的性能計數器值(例如CPU使用率)每2秒連續通過WCF服務,結果將使用ajax顯示在asp頁面上。

現在的問題是,正確的方法也會爲自己創建一個循環。我試圖通過標誌來處理它,但沒有幫助。

注:我也試過WMI,但它沒有返回正確的值。

謝謝

回答