Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a WPF Application Actually Need Application.Run?

Tags:

wpf

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.

like image 510
O. R. Mapper Avatar asked Mar 08 '14 23:03

O. R. Mapper


People also ask

What is needed to run a WPF application?

To run a WPF application, the Microsoft . NET Framework must be installed on the client.

How does WPF application work?

WPF is used to build Windows client applications that run on Windows operating system. WPF uses XAML as its frontend language and C# as its backend languages. WPF was introduced as a part of . NET Framework 3.0 as the Windows library to build Windows client apps and the next generation of Windows Forms.

Can WPF application run in browser?

WPF Browser applications (or XAML Browser applications, or XBAP) are a specific kind of application that are compiled into . xbap extensions and can be run in Internet Explorer.

Can WPF applications be built without using XAML?

For this first program, you won't use Visual Studio's WPF Application template but will instead use the Console Application template. This will allow you to build a bare-bones WPF program without the distractions of XAML and multiple source files. Window myWin = new Window(); // Create the Window object.


1 Answers

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.

like image 77
Jon Avatar answered Sep 30 '22 04:09

Jon