Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

ON SHUTDOWN, On(“ShutDown”)

This clever event handler lets Visual FoxPro applications behave like other Windows applications and respond intelligently when the user closes them in any of a variety of ways, including shutting down Windows, choosing End Task from the Task Manager and closing the app’s main window.

Usage

ON SHUTDOWN [ Command ]
cShutdown = ON("SHUTDOWN")

Back in FoxPro/DOS, we had total control over when and how our applications were closed. Those days are gone forever. In Windows, users can close an application in all kinds of ways. This command helps us get used to it.

Normally, the command you specify is a call to a routine that checks what’s going on and takes appropriate action. For example, you might check for unsaved changes and let the user tell you what to do about them. (That’s how most of the Windows apps we use behave, in fact.)

Omit the command to turn off the shutdown routine and restore the default behavior (which is to present a message, “Cannot Quit Visual FoxPro”). With some system dialogs running, you don’t even get this message—your request to shut down FoxPro is simply ignored. Our impression is that this happens when the running dialog is actually a Windows dialog, not a FoxPro dialog.

ON SHUTDOWN is designed to assume that you are definitely going to go ahead and shut down the application. It’s not always well-behaved when you try to return to your application. Our experience is that you need to be careful about what you put in the shutdown routine. You probably want to be careful out of respect for your users, too. When the user says, “Get me out of here,” you should if you can. It’s okay to stop and ask about saving unsaved work, but it’s pretty obnoxious to ignore the user’s request entirely.

You can use QueryUnload along with ON SHUTDOWN to check the active forms and figure out if you can shut them down before you actually do so. See QueryUnload for an idea of the kind of code you might use there. The example here shows what the shutdown routine might look like.

The ON SHUTDOWN routine can also be a method of an object, such as an application manager object. Then the ON SHUTDOWN call would look like:

ON SHUTDOWN oApp.OnShutDown()

Example

ON SHUTDOWN DO DownShut

PROCEDURE DownShut
* Here's a rough sketch of what you might do.

LOCAL lShutDown, lnCnt
lShutDown = .T.

FOR lnCnt = _SCREEN.FormCount TO 1 STEP -1
   * Backward because we're going to change things.
   IF _SCREEN.Forms[lnCnt].BaseClass = "Form"
      * Skip toolbars
      IF _SCREEN.Forms[lnCnt].QueryUnload()
         _SCREEN.Forms[lnCnt].Release()
       ELSE
          lShutDown = .F.
          EXIT
       ENDIF
   ENDIF
ENDFOR

IF lShutDown
   ON SHUTDOWN
   CLEAR EVENTS
ENDIF

RETURN

See Also

QueryUnload, Quit