ShellExecute

Started by bettman, April 03, 2014, 07:07:13 AM

Previous topic - Next topic

bettman

Greetings,

Is there a way to have ShellExecute wait until the command it's running finishes?

Deana

Yes. Add an AppExist loop immediately after the ShellExecute. For Example:

Code (winbatch) Select
programname = 'notepad.exe'
ShellExecute( programname, '', '', @NORMAL, '' )
While AppExist(programname, 0 , 0  )
    TimeDelay( 0.25 )
EndWhile
Pause( 'ShellExecute with wait', 'Complete' )
Exit
Deana F.
Technical Support
Wilson WindowWare Inc.

snowsnowsnow

That method will run into problems if there are multiple instances of the program running.

I usually use the "259 method" (details available upon request) - which depends on getting the process id (pid) of the spawned process and then waiting for that process to go away.  Of course, the hard part about this is getting the process ID (in a robust way that isn't prone to the same problem as alluded to above).

But, in fact, the "259 method" has a bug - that it can be fooled if a program does (the equivalent of) "exit(3);" (or is it "exit(259);' - I can't remember).  So, the for the truly paranoid, you have to use the "Wait Object" methods (which I've never investigated, so can't comment on).

td

The Framework Class Library (FCL) makes it almost trivial.
Code (winbatch) Select
ObjectClrOption("use","System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")

objProcess   = ObjectClrNew("System.Diagnostics.Process")

; Use ShellExecute to start the app instead of one of the 
; create process functions.
objProcess.StartInfo.UseShellExecute = ObjectType("BOOL", 1)
objProcess.StartInfo.FileName = "Notepad.exe"
objProcess.Start()

; Wait for the process to terminate.
objProcess.WaitForExit()
objProcess.Dispose()
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade