user354583
user354583

Reputation:

Create a WPF window from command prompt application

Is it possible to create WPF window from a command prompt application?

for example I have a MainWindow WPF class with contains the main windows of my application. when I use the following code in my command prompt application I get this error: "The calling thread must be STA".

class Program
{        
    static void Main(string[] args)
    {
         MainWindow main = MainWindow();
         main.Show();
    }
}

I really need to create the window in my command prompt application but i don't know if its possible. please guide me how to do this.

regards

Upvotes: 0

Views: 864

Answers (1)

Quartermeister
Quartermeister

Reputation: 59129

You can correct that error by marking the Main method with a STAThreadAttribute. You will also need to start a message pump by calling Application.Run. For example:

class Program
{
    [STAThread]
    static void Main(string[] args)
    {
         MainWindow main = new MainWindow();
         main.Show();
         new Application().Run();
    }
}

Upvotes: 1

Related Questions