views:

21

answers:

1

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

+2  A: 

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