Search code examples
c#wpfmultithreadingpixelsense

Start SurfaceWindow AND WPF-Window in same Thread


I've got a problem with multi-threading - I am working on a framework which includes two windows: A surfacwindow and a ordinary wpfwindow.

In my first test, the SurfaceWindow was automatically started, due to my visualstudio project choices and in the constructor of my Surface Window I have started the GUI. One Thread for both Windows -> no problems ;)

Know I have to change the order, my starting point is a common .cs Class and I have difficulties with threading... The perfect solution for me would be to start first the SurfaceWindow and second the WPF-Window, but in the same Thread.

But I DON'T want the WPF-Window to be started in the Contructor of the SurfaceWindow, but the same effect!

Code which causes Threading-Problems: (The calling thread can not access this object because the object is owned by another thread.)

 public Constructor()
    {

        Thread t = new Thread(new ThreadStart(StartSurfaceWIndowThread));
        t.SetApartmentState(ApartmentState.STA);
        t.Start();

        startWindow();

    }

    private void StartNewStaThread() {

        AdaptivePrototype.App app = new AdaptivePrototype.App();
        app.InitializeComponent();
        app.Run();

    }

    private void startWindow()
    {
            Thread thread = new Thread(() =>
            {
                WPFkGui w = new WPFGui(this);
                w.Show();

                System.Windows.Threading.Dispatcher.Run();
            });

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();

    }

Now the Code which would be the perfect solution, but it doesn't show the WPFGUI:

 public Constructor()
    {

        Thread t = new Thread(new ThreadStart(StartSurfaceWIndowThread));
        t.SetApartmentState(ApartmentState.STA);
        t.Start();

        startWindow();

    }

    private void StartNewStaThread() {

        AdaptivePrototype.App app = new AdaptivePrototype.App();
        app.InitializeComponent();
        app.Run();
    WPFkGui w = new WPFGui(this);
        w.Show();

    }

I would be happy about any help! Thanks, Chris


Solution

  • I understand that you want 2 windows to show on startup and be able to interact with each other. No fancy threading code is needed.

    Open up your App.xaml file. There is an attrbibute in there for "StartupUri". Remove that - this is basically a shortcut for telling WPF to show a specific window when the app starts up... you can skip this and code in your own startup behavior. To do so, next open up App.xaml.cs and insert something along the lines of:

            Window1 window1 = new Window1();
            Window2 window2 = new Window2(window1);
    
            window1.Show();
            window2.Show();
    

    This assumes of course that your window classes are named Window1 and Window2 and that you added a constructor to Window2 that takes an instance of Window1.