Getting UI dispatcher in class library

rdoubleui picture rdoubleui · May 30, 2011 · Viewed 18k times · Source

I'd like to design a class library and plan to use mutli-threading (i.e. BackgroundWorker). I will have to watch out for the thread context, from which updates are made for fields, if I plan to bind them to the GUI of the library consuming frontend. It's not a good idea to pass the reference of the GUI dispatcher to the library, as I read. But how can I get access to the dispatcher of the application that will use the library? Is this possible?

I tried Application.Current.Dispatcher and added a reference to WindowBase (as I didn't have the possibility to add System.Windows), but still can't resolve the dispatcher object.

Answer

John Leidegren picture John Leidegren · May 30, 2011

The Application class is defined in PresentationFramework.dll. You need to reference that in order to be able to access the dispatcher through Application.Current.Dispatcher.