Equivalent to InvokeRequired in WPF

Is there an equivalent of Form.InvokeRequired in WPF, for example. Dispatcher.InvokeRequired?

+7
source share
4 answers

This is a bit strange as it does not appear in intellisense, but you can use:

var dispatcher = myDispatcherObject.Dispatcher; if (dispatcher.CheckAccess()) { /* ... */ } 

Since all user interface components are inherited from DispatcherObject , this should solve your specific problem, but this does not apply to the user interface thread - it can be used for any dispatcher.

+5
source

Equivalent to Dispatcher.CheckAccess .

+4
source

A possible solution that came to mind is the following:

 if ( Dispatcher.Thread.Equals( Thread.CurrentThread ) ) { Action( ); } else { Dispatcher.Invoke( Action ); } 
+3
source

If you are creating an application for the Windows Store, the above example will not work. Here is an example that works. Change if necessary, of course :)

 /// <summary> /// Updates the UI after the albums have been retrieved. This prevents the annoying delay when receiving the albums list. /// </summary> /// <param name="albums"></param> public void UpdateUiAfterAlbumsRetrieved(System.Collections.ObjectModel.ObservableCollection<PhotoAlbum> albums) { if (!Dispatcher.HasThreadAccess) { Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { ddlAlbums.DataContext = albums; ddlAlbums.IsEnabled = true; tbxProgress.Text = String.Empty; ProgressBar.IsIndeterminate = false; ProgressBar.Visibility = Windows.UI.Xaml.Visibility.Collapsed; }); } else { ddlAlbums.DataContext = albums; ddlAlbums.IsEnabled = true; tbxProgress.Text = String.Empty; ProgressBar.IsIndeterminate = false; } } 
0
source

All Articles