Re: More difficult question - how do I wait until another application closes?



Now I have a Shell command which runs a program EasyBMPtoAVI.exe in
another window (MS-DOS window). That thing typically takes 2 to 100
seconds to complete the run. After that I should delete a lot of files
I create in Visual Basic for EasyBMPtoAVI to process and combine.

How do I make VB watch for EasyBMPtoAVI to finish its task, after
which the code can continue with Kill Image*.bmp and inform me that
the AVI is ready? For the time being, I put a MsgBox command saying
MsgBox "Click OK after EasyBMPtoAVI has completed its task". So once I
click that, the code continues with deleting the myriad bmp's. It
would be nice to do it without the user's involvement, if the code for
that is not complicated.

MICROSOFT 'S OFFICIAL WAY
========================
See this link

http://support.microsoft.com/support/kb/articles/Q129/7/96.asp

Note: This method doesn't use Shell -- it uses CreateProcessA (and provides
example code).


FAST AND DIRTY METHOD
======================
Paste these lines in the (General)(Declarations) section of the form where
the Shell is being called (or remove the Private keywords and put them in a
BAS module if more than one form will use them):

Private Declare Function OpenProcess _
Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle _
Lib "kernel32" _
(ByVal hObject As Long) As Long
Private Declare Function WaitForSingleObject _
Lib "kernel32" _
(ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) As Long

Call your Shell command in this form with the appropriate Shell arguments
placed in the parentheses:

PID = Shell( <<Put Shell Arguments Here>> )

And finally, paste the following IMMEDIATELY after the PID=Shell statement
above (making sure to handle the possible error where indicated; i.e. stop
the code from falling through to your other commands if the Shell failed):

If PID = 0 Then
'
'Handle Error, Shell Didn't Work
'
Else
hProcess = OpenProcess(&H100000, True, PID)
WaitForSingleObject hProcess, -1
CloseHandle hProcess
End If


Rick


.



Relevant Pages

  • Re: Shell Command Doesnt Create TXT file as expected
    ... > Private Declare Function OpenProcess _ ... > Call your Shell command in this form with the appropriate Shell ... > hProcess = OpenProcess ... I also tried to use the suggested MS approach with a different command (I ...
    (microsoft.public.vb.general.discussion)
  • Re: is shelled program still running
    ... This method doesn't use Shell -- it uses CreateProcessA. ... Private Declare Function OpenProcess _ ... If PID = 0 Then ... WaitForSingleObject hProcess, -1 ...
    (comp.lang.basic.visual.misc)
  • Re: SHELL?
    ... The Shell instruction doesn`t do the job since it runs asynchronously. ... Private Declare Function OpenProcess _ ... Lib "kernel32" _ ... WaitForSingleObject hProcess, -1 ...
    (microsoft.public.vb.general.discussion)
  • Re: task list
    ... Shell returns a process id you can use to identify the running process. ... Private Declare Function GetExitCodeProcess Lib "kernel32" (_ ... Public Function Process_Still_ActiveAs Boolean ... Dim lExitCode As Long ...
    (microsoft.public.vb.winapi)
  • RE: how to get vba to wait until a shell command has completed
    ... Private Declare Function GetExitCodeProcess Lib "kernel32" (_ ... Catff.bat is called from Word using the shell command, ... ' Run sample batch file in MS-DOS window. ...
    (microsoft.public.word.vba.general)