# How to Cancel a Process While in a READ

ID: Q124938

2.5x 2.60 2.60a WINDOWS

The information in this article applies to:

- Microsoft FoxPro for Windows, versions 2.5x, 2.6, 2.6a

## SUMMARY

This article explains how to cancel a process that has been started with a READ command. This is functionally similar to being able to press the Cancel button to terminate a print job in any Windows application.

## MORE INFORMATION

A separate Cancel button whose VALID clause contains a CLEAR READ or some other terminating code will not work, since FoxPro cannot initiate the Cancel button while a previous READ is still in effect. The sample code below shows how to work around this limitation.

### Steps to Create a Cancel Button for an Active READ

1. Create a Quick Screen using one of the smaller .DBF files in the FoxPro

       TUTORIAL directory, such as the MOREREG.DBF database file. This file
       ships with approximately 11 records.

2. Near the center of the screen, create a button called Process, and

       assign this button a variable named "scrlrecs" (without the quotation

marks). (SCRLRECS stands for "scroll records.") In the VALID clause for Process, enter the following code:

          SHOW GET scrlrecs DISABLED
          m.cancel = .F.

          DO WHILE m.cancel = .F.
               SHOW GETS LEVEL 1
               SKIP
               IF EOF( )
                    GO TOP
               ENDIF
               @ 8.000,52.000 GET cnclbttn PICTURE "@*HN Cancel" ;
                  SIZE 1.625,8.125,0.500 DEFAULT 1 FONT "MS Sans Serif",8 ;
                  STYLE "B" VALID gocancel( )
               READ CYCLE TIMEOUT .25
          ENDDO

          GO TOP
          SHOW GETS
          SHOW GET scrlrecs ENABLED

          PROCEDURE gocancel
               m.cancel = .T.
               WAIT WINDOW 'Process Terminating' TIMEOUT 2
          RETURN

3. Below the Process button, create another button called Exit and

       assign the Exit button a variable named "extscx" (without the quotation
       marks). (EXTSCX stands for "exit screen.")

4. Select the Terminate READ On Selection check box.

5. Save the screen as MYCANCEL.SCX and generate a MYCANCEL.SPR file.

### Explanation of Procedures

     1. After MYCANCEL.SPR has been run, and Process button chosen, the VALID
        for this button disables the Process button.

     2. The VALID clause creates a memory variable called M.CANCEL and assigns
        it the logical value of false (.F.).

     3. The DO WHILE loop begins running.

     4. As long as M.CANCEL maintains its value of false, the DO WHILE loop
        cycles repeatedly, executing the SHOW GETS, which display the values
        of the fields for the current record.

     5. SKIP controls the advancement of the database record pointer.

     6. The IF statement is executed within the DO WHILE loop to determine if
        the record pointer has moved beyond the last record in the database. If
        so, the pointer is moved back to the first record of the database.

     7. Each pass through the DO WHILE loop redefines an @ ... GET which is
        assigned the variable name CNCLBTTN (cancel button), and displays a
        picture of a button named Cancel.

     8. A VALID is assigned to this @ ... GET statement, which, when executed,
        runs the GOCANCEL procedure.

     9. The READ for this @ ... Get statement is timed out after 1/4 of a
        second if the VALID is not executed.

10. Choosing the Cancel button suspends the DO WHILE loop and runs the

        GOCANCEL procedure. The GOCANCEL procedure does the following:

        a. Changes the value of M.CANCEL to true.
        b. Places a WAIT WINDOW on the desktop for two seconds.
        c. Performs a RETURN to the DO WHILE loop.

11. Because M.CANCEL is now true, the DO WHILE loop ends.

12. The remaining code moves the record pointer to the first record in the

        database, displays the field values of record #1, and reenables the
        Process button.

Additional reference words: FoxWin 2.50 2.50a 2.50b 2.60 2.60a KBCategory: KBSubcategory: FxprgGeneral

    Keywords          : kbcode FxprgGeneral
    Version           : 2.5x 2.60 2.60a
    Platform          : WINDOWS

Last Reviewed: May 22, 1998 © 1999 Microsoft Corporation. All rights reserved. Terms of Use.