2

This code doesn't show the Window, it just closes automatically. Why is this happening?

class Program { [STAThread] static void Main(string[] args) { var window = new MainWindow(); window.ShowDialog(); } } 

I know that you can fix it adding a new Application.Run(window) but I would like to know why it has this behavior and why you have to invoke the Run method over the window instance.

EDIT:

Extending the previous question, I've noticed that this code will work:

  1. Create a new WPF Application.
  2. Go to App.xaml and delete the StartupUri
  3. Modify the App.xaml.cs overriding the method OnStartup

    public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); var window = new MainWindow(); window.Show(); } } 

With this, the window remains open. What's going on under the hood?

3
  • 7
    This is not WPF/XAML, but Windows Forms code. The reason: without Application.Run there is no message loop. Commented Aug 11, 2015 at 18:19
  • 1
    It IS WPF. Window from PresentationFramework. Commented Aug 11, 2015 at 23:16
  • No. There's no Main method in a pure WPF application. You may have a forms application that contains a WPF window, but the code you've shown (Main method) does not exist in WPF. Commented Aug 12, 2015 at 6:35

1 Answer 1

4

Microsoft Windows programs are event-based. They act upon messages that the operating system posts to the main thread of the application. These messages are received from the message queue by the application by repeatedly calling the GetMessage (or PeekMessage) function in a section of code called the "event loop."

When Run is called, Application attaches a new Dispatcher instance to the UI thread. Next, the Dispatcher object's Run method is called, which starts a message loop to process windows messages. Finally, the Dispatcher object calls the Application object's OnStartup method to raise the Startup event.

Without a message loop, the application is unable to support the UI.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @Glen! But I also noticed that I you put just a window.Show inside the Application.OnStartup method (overriding it), the window stays there. What happens in that case? Does Application know when a new window is shown calling Show/ShowDialog? it seems to attach to the window that has been just created.
As far as I know, if you don't call Application.Run, then the OnStartup method will never be called. Is there anything in the Main method when this happens?
Thanks Glen! I've edited the original question adding the code. You can check it for yourself. I have just double-tested with a solution from scratch.
Ah I see what you're doing now. The code that you have added is from a WPF application, whereas the first block of code is from console/winforms/other. The WPF application will call Application.Run for you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.