-1

I used to get Dispatcher using Application.Current.Dispatcher in regular WPF applications. But it doesn't work when I try to get it from different AppDomain because Application.Current returns null there.

Matas
  • 820
  • 9
  • 18
  • Fire an event in the correct AppDomain and use the dispatcher there – Gusman Aug 07 '17 at 17:15
  • Could you please describe what you are trying to achieve? Also as far as I understand you can not access WPF message dispatcher from some other `AppDomain`. You can cross the `AppDomain` boundary with one mechanism or another and pass some information but I suppose you should consider re-thinking why do you need it. – Sergey.quixoticaxis.Ivanov Aug 07 '17 at 17:16
  • 1
    @Sergey.quixoticaxis.Ivanov I am creating an application which supports plugins. These plugins have their own GUI which is displayed in the main window. Plugins are hosted in separate AppDomain from main application. It is sometimes useful to call dispatcher to update some GUI and I am looking for elegant way to receive it like it is done in single-domain apps via `Application.Current.Dispatcher`. – Matas Aug 07 '17 at 17:24
  • 1
    @Matas as far as I remember (it was a long time ago) if you're wrapping your plugins in `MarshalByRef` objects then you can subscribe to the events on proxy object in your main `AppDomain`. I guess it's what @Gusman was advising you to do. To fire an event in remote domain and to handle it (possibly calling dispatcher methods) in the main domain. – Sergey.quixoticaxis.Ivanov Aug 08 '17 at 09:27

1 Answers1

0

When I have needed access to Dispatcher in the past, but Application.Current is null, I have used constructor injection to inject the Window's Dispatcher.

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var objectNeedingDispatcher = new ClassNeedingDispatcher(this.Dispatcher);
    }
}


public class ClassNeedingDispatcher
{
    private readonly Dispatcher _injectedDispatcher;

    public ClassNeedingDispatcher(Dispatcher injectedDispatcher)
    {
        _injectedDispatcher = injectedDispatcher;
    }
}