#40 – Application Lifetime

The diagram below shows a simplified view of the lifetime of a WPF application, as defined by the events that the Application class fires.  (The events are underlined).  Click the diagram to see a full-sized version.

The main events fired from Application include:

  • Startup – Application is starting up.
  • Exit – Fired when an application is shutting down.  Shutdown can’t be canceled.
  • Activated – Fired when an application gets focus, i.e. becomes the foreground application
  • Deactivated – Fired when application loses focus, i.e. is no longer the foreground application
  • DispatcherUnhandledException – Fired when an exception is thrown, but not yet handled.  You can choose to handle the exception or not
  • SessionEnding – Fired when Windows is being shut down–due to either logoff or Windows shutdown.  You can cancel the shutdown sequence.

You can add custom code for any of these events by just overriding the OnEventName method in your Application-derived class, e.g. OnStartup.

#38 – The Application Class

Application is a class that represents a WPF application running as a standalone client application in Windows.  Each running application contains at most a single instance of Application.

WPF applications created using the Visual Studio 2013 New Project wizard will contain the definition of the Application object in the App.xaml file.

<Application x:Class="WpfApplication9.App"


The Application object is responsible for:

  • Managing application lifetime (e.g. responding to startup/shutdown events)
  • Window, property and resource management
  • Command-line processing
  • Navigation