2008-10-21 7 views
5

Dans un UserControl WinForms, je transmettais des données au thread principal de l'interface graphique en appelant this.BeginInvoke() à partir de l'une des méthodes du contrôle. Quel est l'équivalent dans un UserControl Silverlight? En d'autres termes, comment puis-je prendre des données fournies par un thread de travail arbitraire et m'assurer qu'il est traité sur le thread de rappel principal?Dans Silverlight, comment appeler une opération sur le thread Dispatch principal?

Répondre

6

Utilisez la propriété Dispatcher sur la classe UserControl.

private void UpdateStatus() 
{ 
    this.Dispatcher.BeginInvoke(delegate { StatusLabel.Text = "Updated"; }); 
} 
2
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"; 
    } 
Questions connexes