Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Silverlight, how to invoke an operation on the Main Dispatch Thread?

In a WinForms UserControl, I would pass data to the main GUI thread by calling this.BeginInvoke() from any of the control's methods. What's the equivalent in a Silverlight UserControl?

In other words, how can I take data provided by an arbitrary worker thread and ensure that it gets processed on the main displatch thread?

like image 960
Eric Avatar asked Oct 21 '08 17:10

Eric


2 Answers

Use the Dispatcher property on the UserControl class.

private void UpdateStatus()
{
  this.Dispatcher.BeginInvoke( delegate { StatusLabel.Text = "Updated"; });
}
like image 95
Timothy Lee Russell Avatar answered Oct 29 '22 13:10

Timothy Lee Russell


    private void UpdateStatus()
    {
       // check if we not in main thread
       if(!this.Dispatcher.CheckAccess())
       {
          // call same method in main thread
          this.Dispatcher.BeginInvoke( UpdateStatus );
          return;
       }

       // in main thread now
       StatusLabel.Text = "Updated";
    }
like image 2
Ernest Poletaev Avatar answered Oct 29 '22 13:10

Ernest Poletaev