Author Topic: ShellExecute  (Read 3940 times)


  • Newbie
  • *
  • Posts: 42
« on: April 03, 2014, 07:07:13 am »

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


  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: ShellExecute
« Reply #1 on: April 03, 2014, 07:28:17 am »
Yes. Add an AppExist loop immediately after the ShellExecute. For Example:

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


  • Sr. Member
  • ****
  • Posts: 330
Re: ShellExecute
« Reply #2 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).


  • Tech Support
  • *****
  • Posts: 4094
    • WinBatch
Re: ShellExecute
« Reply #3 on: April 04, 2014, 07:12:38 am »
The Framework Class Library (FCL) makes it almost trivial.
Code: Winbatch
ObjectClrOption("use","System, Version=, 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"

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