Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you WAIT for a program or script to finish

Status
Not open for further replies.

greggb

Programmer
Dec 6, 2003
30
US
I have programs that run small DOS batch files and/or small VB scripts and I need to wait for these to finish before the VFP script continues. I can put times to wait but the problem is that each script takes a different amount of time (some alot of time). Is there any way I can let the VFP program know it is finished so it can continue?
 
If the program(s) create a file, you can tell vfp to look for that file, and keep doing so until a (defined by you) timeout, or until the file appears with a time stamp later than when the program started. You'd use adir() in a DO WHILE loop.

There may be some other tricks if you could be more specific about the nature of these other programs, the OS, the user rights level, and the VFP version.

Brian
 
greggb

If you use an appropriate WSH script or the WinAPI call CreateProcess(), you can ensure that control does not return to VFP until the external process is complete.

However, that does not necessarily mean that a file(s) created by the external process either exists or is complete.

As baltman suggests ADIR() in a DO WHILE loop will indicate the existence or otherwise of a file..

If you want to build in error trapping try the following code for the existence of a file. The value of the field USER.timeout can be set by the user in relation to the specification of their PC - shorter for state of the art hardware and longer for legacy hardware.
Code:
[COLOR=blue]lnSeconds = SECONDS()
DO WHILE .T.
[tab]IF SECONDS() > lnSeconds + USER.timeout
[tab][tab]llTimedOut = .T.
[tab][tab]EXIT
[tab]ENDI

[tab]IF ADIR(laTemp,[C:\temp\output.txt]) = 1
[tab][tab]EXIT
[tab]ELSE
[tab][tab]INKEY(0.5)
[tab]ENDI
ENDDO

IF llTimedOut
[tab]MESSAGEBOX([Error - file does not exist!])
ENDI[/color]
The existence of a file does not necessarily mean the file is complete so you can determine that by attempting to open it with FOPEN()
Code:
[COLOR=blue]lnSeconds = SECONDS()
DO WHILE .T.
[tab]IF SECONDS() > lnSeconds + USER.timeout
[tab][tab]llTimedOut = .T.
[tab][tab]EXIT
[tab]ENDI

[tab]lnOpen = FOPEN([C:\temp\output.txt],0)
[tab]IF lnNo = -1
[tab][tab]FCLOSE(lnOpen)
[tab]ELSE
[tab][tab]FCLOSE(lnOpen)
[tab][tab]EXIT
[tab]ENDI
ENDDO

IF llTimedOut
[tab]MESSAGEBOX([Error - file cannot be opened!])
ENDI[/color]

FAQ184-2483 - answering getting answered.​
Chris [pc2]
PDFcommandertm.com
PDFcommandertm.co.uk


 
HI

loShell = CREATEOBJECT("wscript.shell")

lcCmd = GETENV("ComSpec") + [ /C ]
lcBatCmd = lcCmd + [myBatch Command WITH parameters]
** example ..
** lcBatCmd = lcCmd + [CABARC -o -r -p X myBack.CAB]

= loShell.Run(lcBatCmd,1,.t.)

lcBatCmd = lcCmd + [myNextBatchCommand]
= loShell.Run(lcBatCmd,1,.t.)
.. etc..

If you keep the 1 in above statement, the batch command will be visible in a black DOS shell screen.
If you make that to 0 instead of 1, the DOS screen will not be visible.
After the completion of execution only the next lines will be proceeded in loShell.Run() shown above.

:)

____________________________________________
ramani - (Subramanian.G) :)
 
How about a class that's got lost of options to control starting and waiting on both DOS and Windows applications? Go to the UT at click the Downloads picture, then enter "api_apprun" (without the quotes) in the Title box and press enter. You should get back a link to:
"API_APPRUN in .VCX form February 22, 2003
This contains PROCESS.VCX - a visual class library for the API_APPRUN class described in the FAQ. The class gives the ability to run DOS and WinApps from inside VFP using the CreateProcess() API call, and allows you to wait on termination or return immediately, and allows you to examine the execution status and termination code from an executable run by the class.
It takes care of managing the process and thread handles, removing some potential memory bleeds from not properly disposing of proce..."

I've used this in a number of different ways.

Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top