I have a Visual FoxPro 5.0 application that I installed to run as a service on an NT server. Everything works fine except for when the user attempts to log off. During logoff, the VFP/service wants to shut down and fails with the message "Cannot quit FoxPro."
How can I prevent VFP from reacting to the shutdown message being sent from the NT OS?
Visual FoxPro has a command called ON SHUTDOWN [ShutDownProgram/Method], which allows the developer to trap when an application is closed (either at shutdown, or when the close box on the application window is clicked). When the application is closed, the program/method specified (ShutDownProgram/Method) is called. To prevent the "cannot close VFP" message, you need to create a program that handles what you want to occur when the application is shutdown, and point VFP at it, using the ON SHUTDOWN command.
For example, here is a program that tells VFP to call a program named "ShutDownMyApplication" when the application is closed. ShutDownMyApplication() pops up a messagebox, asking whether you want to close the application. If the user chooses Yes, the application is closed; otherwise the application remains open. To test this code, create a program called TestShut and put the following code into it. Then execute the following command in the command window: DO TestShut.
*-- MESSAGEBOX() constants
#DEFINE MB_YESNO 4 && Yes and No buttons
#DEFINE MB_ICONQUESTION 32 && Warning query
#DEFINE MB_DEFBUTTON2 256 && 2nd button default
#DEFINE IDYES 6 && Yes button pressed
*-- Set things so that ShutDownMyApplication
*-- is called when the application is closed
ON SHUTDOWN Do ShutDownMyApplication
*-- Close the application - this will cause
*-- ShutDownMyApplication to fire
lnAnswer = MESSAGEBOX("Close Application", ;
IF lnAnswer = IDYES
*-- this next line is necessary to
*-- prevent ShutDownMyApplication from
*-- firing again because the QUIT command
*-- is the equivalent of closing
*-- the application
*-- this would be a good place to
*-- perform a CLEAR ALL
Please notice how ON SHUTDOWN is turned off before calling QUIT. If you do not do this, you will not be able to close your application (short of ending the task from the NT task manager).
The call to ON SHUTDOWN should be in the main program of your application.