The main processing routine of a Presentation Manager application performs a number of initialization and termination functions for the application, as shown in Figure "Structure of an Application's Main Routine".
The application's main routine registers the application to Presentation Manager, creates the application's message queue and defines private window and message classes for the application before actual processing takes place. Other application-specific initialization processing may also take place at this time, such as the definition of global data items. Note however, that the use of global data increases the interdependence of application modules and reduces the potential for subsequent code reuse. Hence global data should be avoided wherever possible.
The main routine also creates the application's main window. Note that from an application viewpoint, a display window is actually a group of windows that appear and behave as a single unit. Therefore the frame (with its associated title bar, menu bar, etc.) and the client areas are created separately, as shown in Figure "Structure of an Application's Main Routine". This concept is explained in more detail in Window Hierarchy.
During execution of the application, the only function of the main routine is to accept messages from the system queue and route them to window procedures via Presentation Manager. This is performed using a message processing loop, which continues until a message of the system-defined class WM_QUIT is received, at which point the loop terminates and allows the application's termination processing to occur.
Upon termination of the application, the main routine destroys any remaining windows, along with the application's message queue, and deregisters the application before terminating. Any global data items acquired during initialization are also released at this time.
When the user selects "Exit" from the menu bar, or selects the "Close" option on the system menu of the application's main window, the application is not automatically terminated. These messages are passed to the window procedure for that window, and may be processed by the application. The typical action performed by the window procedure in such cases is that a message of class WM_QUIT is posted to the application's message queue, which causes the message processing loop to terminate. Conventions for closing windows and terminating applications are discussed further in Window Closure.
Since Presentation Manager actually informs the application that it is being terminated rather than simply shutting the application down, the application is given a chance to perform any necessary termination processing and exit in an orderly manner. When a window is destroyed, it receives a WM_DESTROY message from Presentation Manager, which may be processed by the window procedure to allow orderly termination of processing. This enables the preservation of data integrity by Presentation Manager applications, which need not rely on the user completing a logical unit of work before terminating; any uncompleted units of work may be placed in an interim storage area on disk, or simply backed out as part of the WM_DESTROY message processing.
This feature is also used by the "Shutdown" procedure of the Workplace Shell. When the user selects "Shutdown" from the menu bar, Presentation Manager posts a WM_QUIT message to the main window of each application executing in the system, informing the application that it is being terminated and allowing any termination processing to take place. This facility allows for an orderly shutdown of the system and preserves the integrity of data objects.