WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: bettman on April 03, 2014, 07:07:13 AM

Title: ShellExecute
Post by: bettman on April 03, 2014, 07:07:13 AM
Greetings,

Is there a way to have ShellExecute wait until the command it's running finishes?
Title: Re: ShellExecute
Post by: Deana on April 03, 2014, 07:28:17 AM
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
Title: Re: ShellExecute
Post by: snowsnowsnow on April 04, 2014, 05:11:26 AM
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).
Title: Re: ShellExecute
Post by: td on April 04, 2014, 07:12:38 AM
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()