开发者

C# WPF dispatcher - can't get it right

I am trying to make a change in the UI and then make my function run here's my code:

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..";
}

the function I want to run after the ui updates / after the ThreadStart 'start' has ended:

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

So what am I supposed to do?


I assume you want to execute some action asynchronous. Right? For this, I recommend using in WPF the 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();  

I hope, this is what your question is about.


Slightly confused on what you are trying to accomplish but I believe this is what you are after...

    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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜