viewpoint-particle

Author Topic: ShellExecute  (Read 3484 times)

bettman

  • Newbie
  • *
  • Posts: 41
ShellExecute
« on: April 03, 2014, 07:07:13 am »
Greetings,

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

Deana

  • 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 )
EndWhile
Pause( 'ShellExecute with wait', 'Complete' )
Exit
Deana F.
Technical Support
Wilson WindowWare Inc.

snowsnowsnow

  • Sr. Member
  • ****
  • Posts: 313
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).

td

  • Tech Support
  • *****
  • Posts: 3453
    • 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=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