Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the right WPF dispatcher in a thread [duplicate]

Tags:

c#

wpf

dispatcher

In the constructor of an object i need to create a WPF mediaElement object:

m_videoMedia = new MediaElement();

but the class can also be instantiated from a other thread so i need to use

Dispatcher.Invoke(DispatcherPriority.Normal,
    (Action)(() => { m_videoMedia = new MediaElement(); })); 

But how can I get the right dispatcher instance in that constructor :s

like image 278
Bert Avatar asked Mar 17 '10 20:03

Bert


1 Answers

You most likely can just use Dispatcher.CurrentDispatcher.Invoke...

However, if for some reason that doesn't work, you could have your class receive a reference to the Dispatcher as part of its constructor. Just pass in Dispatcher.CurrentDispatcher from the UI thread at construction time.

like image 118
Reed Copsey Avatar answered Oct 07 '22 21:10

Reed Copsey