0

I have a WPF log viewer user control that receives log messages via named pipes. The pipe messages are received and processed on a thread pool thread and then I pass it to the UI for display with an Application.Current.Dispatcher.Invoke call. Works like a charm when running in a .Net host application.

When I host the control in an unmanaged C++/CLI application using HwndSource, Application.Current is null. How do I execute code on the UI thread in this scenario?

Gerhard Wessels
  • 681
  • 1
  • 9
  • 18
  • [Walkthrough: Hosting WPF Content in Win32](https://msdn.microsoft.com/en-us/library/ms744829(v=vs.110).aspx) – J... Mar 28 '17 at 15:45
  • @J...: your proposed duplicate does not appear to include information for how to get a `Dispatcher` object in that context. To the OP: your question is incomplete, lacking a good [mcve] that clearly illustrates your scenario. It is possible that if you capture `Dispatcher.CurrentDispatcher` in your WPF object(s) when you are sure they are executing in the UI thread, then you can use that `Dispatcher` object later for your `Invoke()`. Impossible to say for sure without a good MCVE. – Peter Duniho Mar 28 '17 at 17:06
  • C++/CLI is not "unmanaged", C++ on the other hand, is an unmanaged language. – crashmstr Mar 28 '17 at 17:44
  • Possibly related : [How do I host WPF content in MFC Applications?](http://stackoverflow.com/q/829952/327083) – J... Mar 28 '17 at 18:10
  • A few clarifications are in order I think. True, C++/CLI is not unmanaged. I am invoking the the WPF control from an unmanaged MFC (C++) application. I will add an example showing the issue as soon as I have put one together. However, it comes down to the question in my last paragraph. How do you get access to the WPF UI thread when Application.Current is null? – Gerhard Wessels Mar 28 '17 at 19:48
  • I found a way to get this to work. In the user control's constructor I store its dispatcher in a variable where the viewmodels that need to marshal messages onto the UI thread can get hold of it. – Gerhard Wessels Mar 28 '17 at 20:45

0 Answers0