Is there any way to get the UI thread's Dispatcher
when you have no reference to any UI elements?

- 7,972
- 3
- 38
- 61

- 37,042
- 39
- 142
- 198
-
Some more details... you have no reference to the GUI objects or the UI Thread ? What do you intend to do once you have the dispatcher ? – Gishu Apr 18 '10 at 17:12
-
1@Gishu, and for those coming along seven years later like me, you need this when all you have is e.g. an `ObservableCollection` that is being listened to on the UI thread (and thus will fail the check for reentrancy if you try modifying it off the UI thread) yet doesn't expose any dispatchers... in this case you need the UI `Dispatcher` but don't have any UI elements – Matt Thomas Apr 12 '17 at 12:45
2 Answers
You can grab the UI Dispatcher from the static application instance: Application.Current.Dispatcher
You may want to check Application.Current
for null first, as it can be cleared during a shutdown sequence.

- 30,090
- 7
- 62
- 66
-
16Unfortunately this is also null in WinForms, for those unfortunate souls who are hosting WPF elements inside of a WinForms application. – Andrew Garrison Jan 18 '12 at 18:56
-
5@AndrewGarrison, you can manually create the static `Application` object by saying `new System.Windows.Application()`. – Taedrin Oct 06 '15 at 20:08
The following works much better for me when being used in a WinForms application that happens to be also using WPF (in my case Esri's Arcmap.exe)
private System.Windows.Threading.Dispatcher Dispatcher { get; set; }
// I put this in my view model constructor as it MUST be called from UI thread
Dispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher;
The other method (which involved calling new System.Windows.Application()
to populate System.Windows.Application.Current
) caused issues for me every time I opened a new WPF window and then closed it. Not only did this null out System.Windows.Application.Current
, but I could no longer open up new windows as their InitializeComponents()
methods, all failed with:
System.InvalidOperationException: 'The Application object is being shut down.'
So far the new solution is working without those side effects.

- 2,707
- 5
- 31
- 36

- 141
- 2
- 3