views:

210

answers:

2

Is there any way to get the gui thread dispatcher when you have no reference to any elements..

+2  A: 

Use the Dispatcher.CurrentDispatcher property.

Note that you need to get it from the UI thread.

SLaks
so there is no way if you dont have or had a reference to a gui object?
Petoj
You can use that static property without any references to UI objects, as long as you're on the UI _thread_.
SLaks
well in this case im not 100% sure that im on the UI Thread...
Petoj
If you're not sure, you probably are. (Are you using the `Thread` or `ThreadPool` classes?
SLaks
yes in some cases
Petoj
Is your code running there?
SLaks
yes its running on a separate thread but not when the tread is started?
Petoj
Then you'll need to get `CurrentDispatcher` in the UI thread and pass it to the other thread.
SLaks
+2  A: 

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.

Abe Heidebrecht