Freigeben über


Statically getting the UI thread in Silverlight

Like you probably already know, all changes to an object that affects the visual tree has to be done form the UI thread. The Dispatcher, that all DependencyObjects expose, allow the execution of code on this very thread through the BeginInvoke method. Sometimes though no DependencyObject is dynamically available (I just had the case with the internal service of a composite application). How then would one invoke code on the UI thread ?

…by taking advantage of the fact that Silverlight applications only have one visual tree, and thus only one VisualRoot. It, and its Dispatcher are available statically like so :

System.Windows.Deployment.Current.Dispatcher

Please note that this is not valid with WPF, where an application can have multiple dispatchers (for example, multiple Window objects started from different threads) each with its own visual tree and Dispatcher.