Suspend FoxPro execution

Despite all the advances in integration and Automation we often find that the only way to co-ordinate the operation of FoxPro with an external program is for Fox to wait for a file to appear. A snippet like this is a cheap and cheerful way of doing this:

Do While Not File(myFile)
  *-- Do nothing and wait.
Enddo

This though has the huge disadvantage that FoxPro continues to execute. The program will be using processor resources to check whether the file exists several hundred times a second. It would be far better if we could free these resources and let the other program get on with its job more quickly. Fox doesn't have a built-in command to suspend itself but it's very easy to call the Windows API.

Calling the Sleep API

You only need two lines of code; one to declare the API and one to call it.

DECLARE Integer Sleep IN Win32API Integer
Do While .T.
   *-- Suspend execution for 1000 milliseconds
   Sleep(1000)
   If File(lcTempFile)
     Exit
   Endif
Enddo

This code runs in Windows 2000, Windows XP and Windows 7.