Currently I'm aware of the following Dispatcher
objects.
If you have a text view, you can use
IWpfTextView.VisualElement.Dispatcher
.If your class is constructed by MEF (marked with
[Export]
and not directly constructed from your own code), then you can use the fact that the MEF part resolution algorithm and construction occurs on the UI thread, allowing the use ofDispatcher.CurrentDispatcher
. For example:[Export(typeof(ISomeInterface))] public class MyClass : ISomeInterface { private readonly Dispatcher _dispatcher; public MyClass() { _dispatcher = Dispatcher.CurrentDispatcher. } }
You can use
Application.Current.Dispatcher
from any code.
What, if any, is the recommended practice for obtaining a Dispatcher
?
WebEditor.Dispatcher
– AllynSynchronizationContext.Current
instead? – Insectivorousawait
first, thenSynchronizationContext
, withDispatcher
/ISynchronizeInvoke
/CoreDispatcher
last. – InsectivorousIVsOutputWindow.CreatePane
. Using a properDispatcher
is one way to ensure this call is always executed on the correct thread, regardless of the manner in which the MEF part was instantiated.async
/await
are of little use when the operation doesn't start on the correct thread. – GemmuleDispatcher
, and from Kirill's answer below it sounds like MEF is one of those cases. I just discourage its use in the general sense. – Insectivorous