Tech Off Post

Single Post Permalink

View Thread: Open new WPF Window / Multithreading
  • 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();
            }
        }
    }