2012-07-31 36 views
0

我試圖運行一個外觀來將CPU使用情況更新到線程的進度條上。進度條綁定沒有從線程更新

我這裏的代碼是:

private static int _cpuUsage; 
    protected PerformanceCounter cpuCounter; 
    private Thread thread; 

public CPUUsageIndModel() 
    { 
    cpuCounter = new PerformanceCounter 
        {CategoryName = "Processor", CounterName = "% Processor Time", InstanceName = "_Total"}; 
    thread = new Thread(GetCurrentCpuUsage); 
    thread.Start(); 
    } 

public void GetCurrentCpuUsage() 
{ 
    while (true) 
    { 
     _cpuUsage = Convert.ToInt32(Math.Round(cpuCounter.NextValue())); 
     Thread.Sleep(1000); 
    } 
} 

public int GetCPUUsage 
{ 
    get { return _cpuUsage; } 
    set 
    { 
     _cpuUsage = value; 
     NotifyPropertyChanged("_cpuUsage"); 
    } 
} 

現在的事情是我試着開始只用螺紋:

public void GetCurrentCpuUsage() 
{ 
    _cpuUsage = 40; 
} 

並能正常工作,使葉片cpuCounter和循環使用。

任何人都可以指出我可能犯的任何錯誤。

感謝

編輯 - 完整的類和一些小的調整:

public class CPUUsageIndModel : INotifyPropertyChanged 
    { 
     public static int _cpuUsage; 
     protected PerformanceCounter cpuCounter; 
     private Thread thread; 

     public CPUUsageIndModel() 
     { 
      cpuCounter = new PerformanceCounter 
          {CategoryName = "Processor", CounterName = "% Processor Time", InstanceName = "_Total"}; 
      thread = new Thread(GetCurrentCpuUsage); 
      thread.Start(); 
     } 

     public void GetCurrentCpuUsage() 
     { 
      while (true) 
      { 
       CPUUsage = Convert.ToInt32(Math.Round(cpuCounter.NextValue())); 
       Thread.Sleep(1000); 
      } 
     } 

     public int CPUUsage 
     { 
      get { return _cpuUsage; } 
      set 
      { 
       _cpuUsage = value; 
       NotifyPropertyChanged("_cpuUsage"); 
      } 
     } 


     #region INotifyPropertyChanged 
     public event PropertyChangedEventHandler PropertyChanged; 

     private void NotifyPropertyChanged(String info) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(info)); 
      } 
     } 
     #endregion 
    } 
+0

你有沒有考慮的BackgroundWorker和ProgressChanged? – Paparazzi 2012-07-31 21:28:07

回答

1

您需要通知這個改變的WPF運行。爲此,您需要實現INotifyPropertyChanged接口。你似乎試圖在這裏,但你沒有按照正確的方式(你沒有顯示所有相關的代碼,但我相當確定你的實現是不正確的)。

您需要:1。 綁定到一個公共屬性(我們沒有看到這個代碼,你粘貼)從屬性的setter方法 2.發送通知,如果值改變 3.更改通過價值property setter

您可能正在做的第一點,我們沒有看到這一點,但您通知私人領域改變了,並且您應該通知該屬性已更改(NotifyPropertyChanged("GetCPUUsage"))。您也可以通過直接訪問字段(_cpuUsage = 40;)來設置值,並且您應該通過setter(GetCPUUsage = 40;)來執行此操作。

在這種情況下,您的屬性名稱有點奇怪。我會將GetCPUUsage重命名爲CPUUsage,因爲您可以獲取並設置它的價值。前綴獲取應該用於方法,而不是屬性。

+0

感謝您的信息(全班更新問題),我沒有實現'INotifyPropertyChanged',一切工作正常,進度條正確更新,只是當我把所有東西放在循環中,它不工作。 – 2012-07-31 21:36:42

+0

所以你是'CPUUsage',並且你改變了'NotifyPropertyChanged(「_cpuUsage」);''NotifyPropertyChanged(「CPUUsage」);'? – 2012-07-31 21:38:24

+0

好吧,我已經設法解決它,從你的答案,並使用'NotifyPropertyChanged(「CPUUsage」);'做了它的功能如何需要。謝謝您的幫助 :) – 2012-07-31 21:40:51

1

看起來你差不多了,至少從編輯看。只是,您正在提高在字段名稱而不是屬性名稱上更改的屬性。這裏是修復:

public int CPUUsage 
     { 
      get { return _cpuUsage; } 
      set 
      { 
       _cpuUsage = value; 
       NotifyPropertyChanged("CPUUsage"); // Notify CPUUsage not _cpuUsage 
      } 
     } 

和你的綁定應該像{綁定路徑= CPUUsage}