2011-03-03 26 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