Skip Navigation LinksHome > Categories > Code from a Category

Understanding WPF Application Lifecycle

User Name: codelecturer
Name: Mike Chauhan
Contact Me:
Home Page:
6 years of total IT experience including programming, application development, System Maintenance . 3 years of Experienced and expertise in .Net Framework VB, Visual Basic.Net, ASP.Net 2.0, 3.5 and AD... [More]
Viewed Times: 2836
Add Date: 01/01/2012
In this articles we will try to Understand WPF Application Life cycle. What is Application Class, Application life cycle.
1. What is Application Class: When we run any WPF application, it is represented by instance of System.Windows.Application class. The work of this class is to track all open windows in our application, decides when our application will shut down and fires application events that we handle to perform initialization and other work. So every wpf application uses Application class which is why it's important to understand it.

The System.Windows.Application class plays the same role in a WPF application as the System.Windows.Forms.Application class plays in a Windows Forms application.

2. Application life cycle: WPF application undergoes a straight forward life cycle. When we start our application, the application objects are created, and as our application runs, lots of events are fired. Finally, when the application object is released, our application ends.

So let us understand element involved in this life cycle.

A. Creation of Application objects: To understand this, it's good to write the Application class by ourselves. Below I have written an application entry point (a Main () method) that creates a window named Window1 and fires up a new application. For this create a WPF application and add a class called Startup.cs to write below code.

Explanation of code:
1. When we pass a window to the Application.Run() method, that window is set as the main window and exposed to our entire application through the Application.MainWindow property.
2. The Run () method then fires the Application.Startup event and shows the main window.
Well we can accomplish the same result with the other way around of coding like below;

Differences between these two approaches are that in later approach our application continues running until the main window and every other window is closed. At that point, the Run () method returns, and any additional code in our Main () method is executed before the application winds down.

B: Deriving a Custom Application Class: Above approach is not the pattern which visual studio follow when we create a new WPF application, well Instead, Visual Studio derives a custom class from the Application class.
The starting point is a XAML template, which is named App.xaml by default.

The Class attribute is used in XAML to create a class derived from the element. Thus, this class creates a class that derives from Application, with the name TestApplication.App in above example. The application tag not only creates a custom application class, but it also sets the StartupUri property to identify the XAML document that represents the main window.

As with windows, the application class is defined in two separate portions that are fused together at compile time. The automatically generated portion isn't visible in our project, but it contains the Main () entry point and the code for starting the application. It looks something like this:

If we're really interested in seeing the custom application class that the XAML template creates, look for the App.g.cs file in the obj\Debug folder inside our project directory.

Point to remember: The only difference between the automatically generated code shown here and a custom application class that we might create on our own is that the automatically generated class uses the StartupUri property instead of setting the MainWindow property or passing the main window as a parameter to the Run() method.

C. Application Shutdown: Ordinarily, the Application class keeps our application alive as long as at least one window is still open. If this isn't the behavior we want, we can adjust the Application.ShutdownMode. If we're instantiating our Application object by hand, we need to set the ShutdownMode property before we call Run ().
Let us understand ShutdownMode enums

1. OnLastWindowClose: This is the default behavior-our application keeps running as long as there is at least one window in existence. If we close the main window, the Application.MainWindow property still refers to the object that represents the closed window.

2. OnMainWindowClose: our application stays alive only as long as the main window is open. When ShutdownMode is OnMainWindowClose and we close the main window, the Application object will automatically close all the other windows before the Run() method returns.

3. OnExplicitShutdown: The application never ends (even if all the windows are closed) unless we call Application.Shutdown(). This approach might make sense if our application is a front end for a long-running background task.

So when we want to use the OnMainWindowClose approach and we're using the App.xaml file, we need to make this addition in the XAML.

No matter what shutdown method we choose, we can always use the Application. Shutdown() method to end our application immediately.

D. Application events: Initially, the App.xaml.cs file doesn't contain any code. Although no code is required, we can add code that handles application events. The Application class provides a small set of useful events which I have discussed below;

1. Startup: Occurs after the Application.Run() method is called and just before the main window is shown (if we passed the main window to the Run() method).

2. Exit: Occurs when the application is being shut down for any reason, just before the Run() method returns. You can't cancel the shutdown at this point.

3. SessionEnding: Occurs when the Windows session is ending-for example, when the user is logging off or shutting down the computer. You can also cancel the shutdown by setting SessionEndingCancelEventArgs.Cancel to true. If we don't, WPF will call the Application.Shutdown() method when our event handler ends.

Lets have a look on how to use this in code;

4. Activated: Occurs when one of the windows in the application gets activated. This occurs when we switch from another Windows program to this application. It also occurs the first time we show a window.

5. Deactivated: Occurs when a window in the application gets deactivated. This occurs when we switch to another Windows program.

6. DispatcherUnhandledException: Occurs when an unhandled exception occurs anywhere in Your application on the main application thread. The application dispatcher catches these exceptions. By responding to this event, we can log critical errors.

Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.