#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.

Advertisement