Actually not straight forward...
If you're in a modal state - and one example of it is a MessageBox or a report preview, CLEAR EVENTS is just queued for until the modal state ends.
A simple example:
Code:
On Key Label F2 REPORT FORM Home()+"Tools\FileSpec\90frx.frx" PREVIEW
On Key Label F3 CLEAR EVENTS
READ EVENTS
MESSAGEBOX('The End')
Run this, now first press F2, then press F3 while the report preview is active. Exit the report preview and the Messagebox will show. So CLEAR EVENT is not always immediate.
It's still valid to have a READ EVENTS in your VFP project and to have a place where the matching CLEAR EVENTS is run, so that's not saying CLEAR EVENTS doesn't work. A usual place for CLEAR EVENTS is the destroy event of a main application form. Or a menu item for quitting. It's normal that you then will cause it at the right moment, whenever you're in a modal state you can't pick the menu item and can't close the main form, so you never will have the CLEAR in a suspended state that still waits for the ongoing code to stop. So it's all natural and does not need a menu item enforcing the CLEAR EVENTS with prefixed CANCELs, but it does not work ON KEY, as you can be stuck in a modal state.
It's actually the well known problem of exiting an application when the user leaves the workstation with the application in a modal state.
A bit more drastic is
Code:
On Key Label F2 REPORT FORM Home()+"Tools\FileSpec\90frx.frx" PREVIEW
On Key Label F3 QUIT
READ EVENTS
MESSAGEBOX('The End')
Do that and when you press F3 you now get the infamous "Cannot Quit Visual Foxpro"
So lets get even more aggressive:
Code:
On Key Label F2 REPORT FORM Home()+"Tools\FileSpec\90frx.frx" PREVIEW
On Key Label F3 QUIT
On Shutdown QUIT
READ EVENTS
MESSAGEBOX('The End')
Well, in this case F3 will end the report preview, but not the program, you need to press F3 twice to quit. And no, it's not that the first QUIT cancels the PRG and the second exits the IDE, you can check that for yourself. Press F2, then F3 once and the preview vanishes, but click on the Program menu. You'll see the menu items "Cancel" and "Suspend" are active, which means the PRG still runs and now is at the READ EVENTS. The next F3 does QUIT and that does not only end the PRG but also the IDE. And you also see you don't get to the code after READ EVENTS, the messagebox does not appear.
So if you still want something that quits the application in one step you need to go for API and use ExitProcess:
Code:
Declare Long ExitProcess in Win32API Long Exitcode
On Key Label F2 REPORT FORM Home()+"Tools\FileSpec\90frx.frx" PREVIEW
On Key Label F3 ExitProcess()
READ EVENTS
MESSAGEBOX('The End')
Well, it is drastic, it exits the process at any time.
For something that's less drastic you can use CANCEL:
Code:
On Key Label F2 REPORT FORM Home()+"Tools\FileSpec\90frx.frx" PREVIEW
On Key Label F3 CANCEL
READ EVENTS
MESSAGEBOX('The End')
It's like QUIT again, as the first CANCEL ends the preview but not the PRG, but the second F3 will now cancel the PRG, but then it's less aggressive as QUIT and does not quit the IDE. It's still not getting to the point after READ EVENTS, as that needs the CLEAR EVENTS. So ideally you'd have something that does CANCEL until the stack is on the level of the READ EVENTS and then can do CLEAR EVENTS. It may need more than one cancel, though. It's really not easy to get to after READ EVENTS.
The simplest for you as developer debugging is establishing a key for SUSPEND, then you have anything at your hand, like the debugger, to do further steps: So this would be my setup for developing:
Code:
On Key Label F2 REPORT FORM Home()+"Tools\FileSpec\90frx.frx" PREVIEW
On Key Label F3 SUSPEND
READ EVENTS
MESSAGEBOX('The End')
When in suspended mode you can use command window and debugger, you can do CANCEL, for example and then CLEAR EVENTS and are using the intended path and see the messagebox, which in a real scenario may instead be code that handles all open transactions and forms.
You can also do things like SET ESCAPE ON at runtime and allow end users of an EXE to cancel code by pressing ESC. It's not a good thing to allow, therefore the default setting of it is OFF at runtime. Just like there are defaults for all environment settings that can differ in IDE and EXE (runtime mode does not have the opposite settings for everything, though).
For developing and debugging I have SUSPEND on a function key, but not set this up for runtime mode, so you can have your developer belt on with
Code:
If _vfp.StartMode = 0
On Key Label F12 SUSPEND
Endif
Makes it easy to remember for me as F12 also goes into developer tools/mode in Web browsers, too. CANCEL is possible by ESC key, so no need to define this.
If you want to you can of course allow an ExitProcess at runtime, it does not do more harm than using the task manager to kill the vfp process, which can only be taken away from users under special circumstances like group policies removing that or running Windows in kiosk mode. It shouldn't be made too easy to happen accidentally hitting some function key alone, so perhaps make it a hotkey like ALT+F12. It doesn't have to be ON KEY, but can be a menu item with hot key. Be aware that this solution does not run your code after READ EVENTS. So instead of calling ExitProcess you may set up a function key to call your finalization code, which ends in ExitProcess.
Chriss