2011-03-03 36 views
0

我試圖在用戶界面的變化,然後讓我的函數運行 這裏是我的代碼:C#WPF調度員 - 不能得到它的權利

private void btnAddChange_Document(object sender, RoutedEventArgs e) 
{ 
    System.Threading.ThreadStart start = delegate() 
    { 
     // ... 

     // This will throw an exception 
     // (it's on the wrong thread) 
     Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(changest)); 

     //this.BusyIndicator_CompSelect.IsBusy = true; 

     //this.MainWindow_parent.BusyIndicator_MainWindow.IsBusy = true; 
    }; 
    // Create the thread and kick it started! 
    new System.Threading.Thread(start).Start(); 
} 

public void changest() 
{ 
    this.BusyIndicator_CompSelect.IsBusy = true; 
    this.MainWindow_parent.BusyIndicator_MainWindow.IsBusy = true; 
    t = "Creating document 1/2.."; 
} 

我想在UI後運行功能更新/ ThreadStart「開始」結束後:

string x = ""; 
for(int i =0;i<=1000;i++) 
{ 
    x+= i.ToString(); 
} 
MessageBox.Show(x); 

那麼我該怎麼辦? 先進的感謝, 丁。

回答

2

我假設你想執行一些異步的操作。對?對於這一點,我建議使用WPF中的BackgroundWorker -class:

BackgroundWorker bgWorker = new BackgroundWorker() { WorkerReportsProgress=true}; 
bgWorker.DoWork += (s, e) => {  
    // Do here your work 
    // Use bgWorker.ReportProgress(); to report the current progress 
}; 
bgWorker.ProgressChanged+=(s,e)=>{  
    // Here you will be informed about progress and here it is save to change/show progress. 
    // You can access from here savely a ProgressBars or another control. 
}; 
bgWorker.RunWorkerCompleted += (s, e) => {  
    // Here you will be informed if the job is done. 
    // Use this event to unlock your gui 
}; 
// Lock here your GUI 
bgWorker.RunWorkerAsync(); 

我希望,這是你的問題是什麼。

+0

這裏是我試過(似乎沒有工作= /):bgWorker.DoWork + =(s,ev)=> {bgWorker.ReportProgress();報告當前進度 this.BusyIndi​​cator_CompSelect.IsBusy = true; this.MainWindow_parent.BusyIndi​​cator_MainWindow.IsBusy = true; t =「正在創建文檔1/2 ..」; bgWorker.ReportProgress(100); }; bgWorker.RunWorkerCompleted + =(s,ev)=> this.MainWindow_parent.MyAddDocumentFunc(lbOS.SelectedValue.ToString(),lbVersion.SelectedValue.ToString()); }; bgWorker.RunWorkerAsync(); – dinbrca 2011-03-03 20:47:42

+0

@dinbrca:會發生什麼?你有哪些異常/錯誤行爲? – HCL 2011-03-03 21:02:37

+0

沒有發生異常,雖然它看起來像IsBusy值不會改變=/ – dinbrca 2011-03-03 22:16:01

1

您所要完成的輕度的意識模糊,但我相信這是你所追求的......

private void btnAddChange_Document(object sender, RoutedEventArgs e) 
    { 
     System.Threading.ThreadStart start = delegate() 
     { 
      //do intensive work; on background thread 
      string x = ""; 
      for (int i = 0; i <= 1000; i++) 
      { 
       x += i.ToString(); 
      } 

      //done doing work, send result to the UI thread 
      Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, 
       new Action<int>(changest)); 

     }; 

     //perform UI work before we start the new thread 
     this.BusyIndicator_CompSelect.IsBusy = true; 
     this.MainWindow_parent.BusyIndicator_MainWindow.IsBusy = true; 
     t = "Creating document 1/2.."; 

     //create new thread, start it 
     new System.Threading.Thread(start).Start(); 
    } 

    public void changest(int x) 
    { 
     //show the result on the UI thread 
     MessageBox.Show(x.ToString()); 
    } 
+0

它似乎首先創建一個新文檔,然後它將IsBusy值更改爲true。因爲BusyIndi​​cator在文檔創建後出現。我使用「Extended WPF Toolkit」的BusyIndi​​cator。 – dinbrca 2011-03-03 22:12:28