Hi guys, I seem to be having a crappy day, I can't figure anything out!! lol
I have an app that has a WCF service.
When a message arrives via my service, I fire an event called NewMessage()
In the event Handler, I want to open a new WPF window to display the message contents.
The problem is, the event is obviously not firing from the UI thread, and therefore my handler cannot create a new window.
However I can't do a Dispatcher.Invoke, because I have no object on which to call the dispatcher if that makes sense?
How can I achieve this?
-
-
jh71283 wrote:Hi guys, I seem to be having a crappy day, I can't figure anything out!! lol
I have an app that has a WCF service.
When a message arrives via my service, I fire an event called NewMessage()
In the event Handler, I want to open a new WPF window to display the message contents.
The problem is, the event is obviously not firing from the UI thread, and therefore my handler cannot create a new window.
However I can't do a Dispatcher.Invoke, because I have no object on which to call the dispatcher if that makes sense?
How can I achieve this?
Application.Current.MainWindow? -
Hmm, I don't have Application.Current.
Maybe i should point out that the app is winforms based, but has an increasing number of WPF windows within. -
I can't answer you specifically, as I've not done anything using WCF or WPF, though it's interesting that your event is not being fired using the Application's UI thread - a configuration issue ?
Anyway, generally, with any CallBack mechanism, there are usually parameters passed in which either you or the framework supplies. So I'd be looking at what you can extract from the incomming (event) parameters. eg a sender parameter (object/class).
For example, in the recent thread using RegisterHotkey, you have to tell RegisterHotKey what to pass back when it initiates your callback. I'm sure there would be something similar here, though in this case it should be something from which you can derive back to your UI thread. -
Well one of the main things, if your pushing these new forms off into their own threads, you should make sure their thread has a MessageQueue handler, such that each of these new Form-Threads can handle their own messages. I know this is true with WinForms, and likely remains true for WPF and they still need to process messages.
To do that you should look into Application.Run( ... ); -
Try -
System.Windows.Threading.Dispatcher currentDispatcher = System.Windows.Application.Current.Dispatcher;
currentDispatcher.Invoke(...);
You must have a reference to the PresentationFramework.dll though, which I assume you already do. -
Object reference not set to an instance of an object.
System.Windows.Application.Current is nothing
Bearing in mind that the application is winforms based, what is the winforms equivalent to System.Windows.Application.Current.Dispatcher? -
jh71283 wrote:Object reference not set to an instance of an object.
System.Windows.Application.Current is nothing
Bearing in mind that the application is winforms based, what is the winforms equivalent to System.Windows.Application.Current.Dispatcher?
Below is a code example on how to do this. Basically you need to create the Window in a delegate that is passed to the Invoke method on any control in your Windows Forms solution.
using System;
using System.Windows.Forms;
using System.Threading;namespace WPFHost
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void button1_Click(object sender, EventArgs e)
{
Thread thrd = new Thread(new ThreadStart(
delegate() {
this.Invoke(new MethodInvoker(CreateWindow));
}));
thrd.Start();
}public void CreateWindow()
{
System.Windows.Window wnd = new System.Windows.Window();
wnd.ShowDialog();
}
}
}
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.