Controlling the Event Loop

Once the environment is set up and you've displayed the initial user interface, you're ready to establish an event loop to wait for user interaction.

To control the event loop

  • Issue the READ EVENTS command, which causes Visual FoxPro to begin processing user events such as mouse clicks and keystrokes.

It is important to place the READ EVENTS command correctly in your main file, because all processing in the main file is suspended from the time the READ EVENTS command is executed until a subsequent CLEAR EVENTS command is issued. For example, you might issue a READ EVENTS command as the last command in an initialization procedure, executed after the environment has been initialized and the user interface displayed. If you don't include the READ EVENTS command, your application will return to the operating system after running.

After the event loop has been started, the application is under the control of the user interface element that was last displayed. For example, if the following two commands are issued in the main file, the application displays the form Startup.scx:

DO FORM STARTUP.SCX
READ EVENTS

If you don't include a READ EVENTS command or its equivalent in your main file, your application will run properly from the Command window within the development environment. However, when you run it from your menu or main screen, the application will appear briefly, then quit.

Your application must also provide a way to end the event loop.

To terminate the event loop

Typically, you issue the CLEAR EVENTS command from a menu or a button in a form. The CLEAR EVENTS command suspends the event processing for Visual FoxPro and returns control to the program that issued the READ EVENTS command that started the event loop.

For a simple program example, see Structuring a Program as a Main File.

Caution   You need to establish a way to exit the event loop before you start it. Make sure your interface has a mechanism (such as an Exit button or menu command) to issue the CLEAR EVENTS command.

Restoring the Original Environment

To restore the original value of saved variables, you can macrosubstitute them into the original SET commands. For example, if you saved the SET TALK setting into the public variable cTalkVal, issue the following command:

SET TALK &cTalkval

Note   Variable names used with macro substitution shouldn't contain the "m." prefix because the period assumes a variable concatenation and will produce a syntax error.

If you initialized the environment in a different program than the one in which you are restoring it — for example, if you initialize by calling one procedure, but restore the environment by calling another — be sure you can access the values you stored. For example, store the values to restore in public variables, custom classes, or as properties of an application object.

See Also

Initializing the Environment | Structuring a Program as a Main File | Compiling an Application | READ EVENTS | Adding Files to a Project