locked
Open a window from console applications RRS feed

  • Question

  • Hi

    I'm writing a console application and I want to open a new window according to the user input.
    When it tries to open the new window by
    System.Windows.Window myWindow = new System.Windows.Window();
    I get an exception:
    An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationCore.dll
    Additional information: The calling thread must be STA, because many UI components require this.

    How can I solve it?

    I tried to add [STAThread] before the Main function of the console application and then I didn't get the exception, but the new window wasn't actually opened.

    Thanks,
    Moti
    Thursday, March 11, 2010 8:51 AM

Answers

  • Thanks.

    I have already added "SAThread", called show method and added the required references.
    for some reason the new window is opened in the background and I can see it in the taskbar and the task manager, but it freezes and I can't see or use the window itself.

    however, I solved the problem by different implementation. I use WPF application and change the main window according to the use input and it seems ok.

    Thanks!
    Moti


    • Marked as answer by Linda Liu Wednesday, March 17, 2010 9:00 AM
    Thursday, March 11, 2010 3:38 PM
  • Good job!

    We can reference to the code which WPF application generate automatically (like App.g.cs in \obj\Debug folder usually). Below is my code using a application object to run the WPF window.

            [STAThread]
            static void Main(string[] args)
            {
                Application app = new Application();
                System.Windows.Window myWindow = new System.Windows.Window();
                myWindow.Show();
    
                app.Run();
                Console.ReadLine();
            }

    Best day, Best life
    • Proposed as answer by Jarrey Wednesday, March 17, 2010 1:43 AM
    • Marked as answer by Linda Liu Wednesday, March 17, 2010 9:00 AM
    Friday, March 12, 2010 1:36 AM

All replies

  • Hi motimark,

    If you want call a window from console, the main function must be "SAThread". Do you miss to call show method of myWindow? And did you already add below assemblies to your project if you want a WPF window?

    PresentationCore - PresentationCore.dll
    PresentationFramework - PresentationFramework.dll
    WindowsBase - WindowsBase.dll

    Hope this helps.


    Best day, Best life
    Thursday, March 11, 2010 9:35 AM
  • Thanks.

    I have already added "SAThread", called show method and added the required references.
    for some reason the new window is opened in the background and I can see it in the taskbar and the task manager, but it freezes and I can't see or use the window itself.

    however, I solved the problem by different implementation. I use WPF application and change the main window according to the use input and it seems ok.

    Thanks!
    Moti


    • Marked as answer by Linda Liu Wednesday, March 17, 2010 9:00 AM
    Thursday, March 11, 2010 3:38 PM
  • Good job!

    We can reference to the code which WPF application generate automatically (like App.g.cs in \obj\Debug folder usually). Below is my code using a application object to run the WPF window.

            [STAThread]
            static void Main(string[] args)
            {
                Application app = new Application();
                System.Windows.Window myWindow = new System.Windows.Window();
                myWindow.Show();
    
                app.Run();
                Console.ReadLine();
            }

    Best day, Best life
    • Proposed as answer by Jarrey Wednesday, March 17, 2010 1:43 AM
    • Marked as answer by Linda Liu Wednesday, March 17, 2010 9:00 AM
    Friday, March 12, 2010 1:36 AM