Legacy Documentclose button

Important: The information in this document is obsolete and should not be used for new development.

Previous Book Contents Book Index Next

Inside Macintosh: Processes
Chapter 2 - Process Manager / Using the Process Manager


Terminating an Application

The Process Manager automatically terminates a process when the process either exits its main routine or encounters a fatal error condition (such as an attempt to divide by 0). When a process terminates, the Process Manager takes care of any required cleanup operations; these include removing the process from the list of open processes and releasing the memory occupied by the application partition (as well as any temporary memory the process still holds). If necessary, the Process Manager sends an Application Died event to the process that launched the one about to terminate.

Your application can also terminate itself directly by calling the ExitToShell procedure. In general, you need to call ExitToShell only if you want to terminate your application without having it return from its main routine. This might be useful when your initialization code detects that some essential system capability is not available (for instance, when the computer running a stereo sound-editing application does not support stereo sound playback). Listing 2-3 shows one way to exit gracefully in this situation.

Listing 2-3 Terminating an application

PROCEDURE CheckForStereoSound;
VAR
   myErr:      OSErr;            {result code from Gestalt}
   myFeature:  LongInt;          {features bit flags from Gestalt}
   myString:   Str255;           {text of alert message}
   myItem:     Integer;          {item returned by StopAlert}
CONST
   kAlertBoxID    = 128;         {resource ID of alert template}
   kAlertStrings  = 128;         {resource ID of alert strings}
   kNoStereoAlert = 5;           {index of No Stereo alert text}
BEGIN
   myErr := Gestalt(gestaltSoundAttr, myFeature);
   IF myErr = noErr THEN
      IF BTst(myFeature, gestaltStereoCapability) = FALSE THEN
         BEGIN
            GetIndString(myString, kAlertStrings, kNoStereoAlert);
            ParamText(myString, '', '', '');
            myItem := StopAlert(kAlertBoxID, NIL);
            ExitToShell;         {exit the application}
         END
   ELSE
      DoError(myErr);
END;
The procedure CheckForStereoSound defined in Listing 2-3 checks whether the computer supports stereo sound playback. If not, CheckForStereoSound notifies the user by displaying an alert box and terminates the application by calling ExitToShell.

Note
The ExitToShell procedure is the only means of terminating a process. It is always called during process termination, whether by your application itself, the Process Manager, or some other process. ·
If your application launches another application that terminates, either normally or as the result of an error, the Process Manager can notify your application by sending it an Application Died event. To request this notification, you must set the acceptAppDied flag in your application's 'SIZE' resource. (For a complete description of the 'SIZE' resource, see the chapter "Event Manager" in Inside Macintosh: Macintosh Toolbox Essentials.)
Application Died--inform that an application has terminated
Event IDkAEApplicationDied
Required parameters 
KeywordkeyErrorNumber
Descriptor typetypeLongInteger
DataA sign-extended OSErr value. A value of noErr indicates normal termination; any other value indicates that the application terminated because of an error.
KeywordkeyProcessSerialNumber
Descriptor typetypeProcessSerialNumber
DataThe process serial number of the application that terminated.
Requested actionNone. This Apple event is sent only to provide information.

The Process Manager gets the value of the keyErrorNumber parameter from the system global variable DSErrCode. This value can be set either by the application before it terminates or by the Operating System (when the application terminates as the result of a hardware exception or other problem).


Previous Book Contents Book Index Next

© Apple Computer, Inc.
17 JUN 1996