0

I have a WPF UserControl that is being tested in both a WPF app and WinForms app. The control is made up of other controls, mainly a map control. Work is done inside the UserControl using Tasks and doing work on the map control by using Application.Current.Dispatcher.Invoke.

Everything works fine when hosting this control in a WPF app, but in the WinForms app, a NullReference exception is thrown anytime the Dispatcher.Invoke is used. In fact, Application.Current is always null in the WinForms app.

I've seen this: https://stackoverflow.com/a/14753962/2480598 which suggests:

if ( null == System.Windows.Application.Current )
{
   new System.Windows.Application();
} 

But what if I don't have control over the WinForms app? Is there no other way to handle this?

pfinferno
  • 1,779
  • 3
  • 34
  • 62
  • 1
    Have a look at this [thread](https://stackoverflow.com/questions/303116/system-windows-threading-dispatcher-and-winforms) as well. You can try to use `Dispatcher.CurrentDispatcher` – Pavel Anikhouski Jun 26 '19 at 11:48
  • 1
    The `DispatcherObject` base class has a `Dispatcher` property. Try to use this one instead of `Application.Current.Dispatcher`. – mm8 Jun 26 '19 at 11:55
  • Thank you to both. Using the `Dispatcher.CurrentDispatcher` seemed to do the trick. – pfinferno Jun 26 '19 at 11:58

0 Answers0