Eric
Eric

Reputation: 11662

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?

Upvotes: 5

Views: 3635

Answers (2)

Ernest Poletaev
Ernest Poletaev

Reputation: 590

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

Upvotes: 2

Timothy Lee Russell
Timothy Lee Russell

Reputation: 3748

Use the Dispatcher property on the UserControl class.

private void UpdateStatus()
{
  this.Dispatcher.BeginInvoke( delegate { StatusLabel.Text = "Updated"; });
}

Upvotes: 6

Related Questions