Tech Off Thread

8 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Open new WPF Window / Multithreading

Back to Forum: Tech Off
  • User profile image
    jh71283

    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?

  • User profile image
    wkempf

    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?

  • User profile image
    jh71283

    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.

  • User profile image
    RichardRudek

    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.

  • User profile image
    Isshou

    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( ... );

  • User profile image
    nightski

    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.

  • User profile image
    jh71283

    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?

  • User profile image
    nightski

    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();
            }
        }
    }

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.