Does a WPF Application Actually Need Application.Run?

So far, I had assumed that a call to System.Windows.Application.Run in a WPF application would start its main message loop and thus provide the UI with its "life". At least, that is what happens in WinForms and Gtk#, and the same thing seems to be suggested for WPF in this question, that blogpost, and ultimately, the docs themselves (indirectly, via Dispatcher.Run ).

The documentation for WPF's Application.Run emphasizes how important it is to invoke Run :

Run is called to start a WPF application. (...) if you define your Application using code, you will need to explicitly call Run.

It then presents an example that tries to be compelling in its demonstration of how necessary the invocation of Run is. And indeed - commenting out the line

app.Run();

from that example results in an application that just displays a console window for a moment and then quits immediately.

But, well, conveniently, the window in that example is created and displayed in the OnStartup method of the Application object, which gets invoked after calling Run . After moving the window-related code into the Main method, the window is at least displayed for a moment:

[STAThread]
public static void Main()
{
    CustomApplication app = new CustomApplication();
    //app.Run();

    Window window = new Window();
    window.Show();
}

Obviously, that is because with window being non-modal, execution continues right after the window is displayed and the application reaches its end right away. So, if we show window modally, things look better:

[STAThread]
public static void Main()
{
    //CustomApplication app = new CustomApplication();
    //app.Run();

    Window window = new Window();
    window.ShowDialog();
}

Now, the window is displayed just as usual. Note that I have meanwhile commented out not only the Run invocation, but the instantiation of the Application object altogether. Checking with some stepwise execution confirms that Application.Current is always null , so there is no Application instance throughout the lifetime of this application and Application.Run is effectively never called - yet, the application appears to be responsive.

Does ShowDialog perform its own invocation of Dispatcher.Run ? If so, why the insistence in the docs to call Application.Run ? Is there anything else going to break that I didn't encounter in this minimal test if I do not have an Application object or an invocation of Application.Run in my code?

Some context: I am in a situation where by default, there is no Application instance around (launching a new AppDomain and serving my WPF-based GUI from there - Application.Current is null by default in the secondary AppDomain , and I am bypassing the default entry point in my primary AppDomain as I do not want the primary AppDomain to host the main window, hence I have to decide whether or not to call Application.Run myself there). Therefore, I am attempting to find out whether calling Application.Run can cause any problems, or whether not calling it will be painful in any way.


Yes, ShowDialog does start its own dispatcher loop -- otherwise the window would not be able to process window messages and it would either close immediately or "hang". It does this by creating a new DispatcherFrame and calling Dispatcher.PushFrame on it.

Application.Run doesn't do anything super important other than creating a window and synchronously running a dispatcher loop, so there should not be any problems with what you are doing here.

链接地址: http://www.djcxy.com/p/78046.html

上一篇: Mongodb真正的基本用例

下一篇: WPF应用程序实际上是否需要Application.Run?