Y
Y
Yaroslav Ryazanov2016-12-28 15:49:10
WPF
Yaroslav Ryazanov, 2016-12-28 15:49:10

How to run WPF application from console?

Good afternoon.
What is available:

  • One Solution
  • There are two projects in it. One of which is console, the other is WPF

It is required to run the WPF application in a new thread from the console.
I found a way on the Internet through System.Windows.Application, but this does not exist.
Then in the WPF application I created a new method, so to speak, the main one.
And ran it in a new thread like this:
// Наш класс WPF приложения.
    MainWindow WPF = new MainWindow();
    Thread WPFTh = new Thread(new ThreadStart(WPF.MainWindows));
    WPFTh.Start();  // Запускаем поток

In this method (the main one), the components are initialized, as in the constructor of a regular WPF application. But nothing happens. The application does not start.
What's the trouble?
--ADD--:
Tried using the show method to open the window in a new thread.
MainWindow WPF = new MainWindow();
Thread WPFTh = new Thread(new ThreadStart(WPF.Show));
WPFTh.Start();

But the following error pops up:
"An unhandled exception of type 'System.InvalidOperationException' in WindowsBase.dll
Additional information: The calling thread cannot access this object because another thread is the owner of this object."

Opened without a new stream. The console works, but the window doesn't. It opened, but it remains to hang not working.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sumor, 2016-12-29
@BRUC

using System;

class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        Console.WriteLine("Starting WpfApplication1.exe...");

        var domain = AppDomain.CreateDomain("WpfApplication1Domain");
        try
        {
            domain.ExecuteAssembly("WpfApplication1.exe");
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        finally
        {
            AppDomain.Unload(domain);
        }

        Console.WriteLine("WpfApplication1.exe exited, exiting now.");
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question