How to Pause Program Execution During a RUN CommandLast reviewed: April 30, 1996Article ID: Q110117 |
The information in this article applies to:
SUMMARYWhen the RUN command is used to execute an MS-DOS command in an MS-DOS session window under 16-bit versions of Microsoft Windows (Windows versions 3.x), execution of the FoxPro program that issued the RUN command will continue immediately after the RUN command is issued. This article describes an approach that can be used to cause program execution to "wait" until the MS-DOS command has finished executing.
MORE INFORMATIONThe RUN command creates a virtual MS-DOS session under Microsoft Windows. When the RUN command is executed in a FoxPro program, it causes this virtual MS-DOS session to be created, then passes the appropriate commands to the MS-DOS environment. FoxPro has no control over the virtual MS-DOS session; it is a separate process that is under the control of Microsoft Windows. Since it is a separate process, any lines following the RUN command in a FoxPro program will be executed as soon as the RUN command has finished creating the MS-DOS session. In many cases, this is not a problem. In some cases, however, you may want to cause execution of the FoxPro program to stop until the MS-DOS commands have finished executing. This article demonstrates how to call the Windows API function FindWindow() to determine whether or not the MS-DOS session is active and uses a loop to prevent program execution from continuing until the commands executed in the MS-DOS session are finished and the MS-DOS session is therefore terminated. Before using the code sample provided in this article, you must create a special program information file (PIF). NOTE: The following steps can also be used to check if a Windows application has finished running. Omit the sections that pertain to using a PIF file and running a batch file. Then, instead of running the PIF file, run the Windows-based application. Be sure to assign the appropriate mWindow and mClassName values for the Windows-based application. NOTE: If you have Microsoft Visual C++ for Windows, you can use the Spy utility to determine the class name for a particular window.
Creating a PIF File to Be Used by the Sample CodeNOTE: These instructions assume that you have not modified the FOXRUN.PIF file that ships with FoxPro for Windows. If you have modified this file, you may need to reinstall FoxPro for Windows in order to obtain a copy of the default FOXRUN.PIF file that ships with FoxPro for Windows.
PROCEDURE myrun CLEAR * This line calls the procedure "makebat", which creates a batch * file to execute MS-DOS commands. This functionality is * included in this sample so that a full-functioned demonstration * can be provided. In practice, the line "DO makebat" and the * procedure "makebat" are not necessary. In normal usage, you * would create a batch file that performs the required actions * and include it with the application. DO makebat * We need FOXTOOLS.FLL to make calls to the Microsoft Windows API SET LIBRARY TO SYS(2004)+"FOXTOOLS.FLL" * Register the FindWindow function FindWindow = REGFN("FindWindow","@C@C","I") * The text assigned to the memory variable "mWindow" is the * title of the window created by the PIF file. You can change * the title of this window by changing the text in the Window * Title field (see instructions for editing the PIF file). If * you change the contents of the Window Title field in the PIF * file, the same window title should be assigned to the mWindow * variable below. mClassName should be "tty" mWindow = "FoxPro Run Command" mClassName = "tty" * The PIF file should be contained in the FoxPro for Windows * directory. We do not know what directory this program may be * running from. The use of SYS(2004) ensures that this program * will find the PIF file. In actual practice, macro substitution * can be avoided if the path to the PIF file is known. For * example: RUN /n \path\myrun.pif mFile = sys(2004)+"myrun.pif" RUN /n &mFile * Get the handle to the window titled "FoxPro Run Command". * This window is opened when the RUN command is executed. mWindowHandle = CALLFN(FindWindow,@mClassName,@mWindow) * This loop prevents program execution from continuing until * the RUN command is finished. When the RUN command is finished, * the FoxPro Run Command window will disappear. When this occurs, * the handle (mWindowHandle) will be 0. While the Window exists, * the value of mWindowHandle will not be 0. Inside a loop, we * continuously call FindWindow until a 0 is returned, indicating * that the RUN command has finished execution. DO WHILE mWindowHandle <> 0 * In actual usage, the following line can be removed. It is * included here to provide visual feedback. ? "The RUN Command is executing. Window handle is:"; +LTRIM(STR(mWindowHandle)) mWindowHandle = CALLFN(FindWindow,@mClassName,@mWindow) = INKEY(1) ENDDO * In actual usage, the following line can be removed. It is * included here to provide visual feedback. ? "The RUN Command is finished" * The FOXTOOLS library is no longer needed SET LIBRARY TO RETURN PROCEDURE makebat * This procedure is used to create a text file (batch file) * that will be used by the .PIF file to execute MS-DOS * commands. DELETE FILE mybatch.bat h = FCREATE("Mybatch.BAT") = FWRITE(h,"CHKDSK"+CHR(13)+CHR(10)) = FWRITE(h,"DIR"+CHR(13)+CHR(10)) = FWRITE(h,"DIR"+CHR(13)+CHR(10)) = FCLOSE(h) RETURN |
Additional reference words: FoxWin VFoxWin 2.50 2.50a 2.50b 2.60 2.60a 3.00
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |