0

Apologies is this has already been answered, I cannot find it. I have a requirement to launch an external process from a vb6 app and wait for that process to finish before continuing. Simple enough. However the process I need to launch in turn launches a child process then exits. I need to wait for the child process to complete (and and other child processes)

Existing code:

Private Const WAIT_INFINITE = -1&
Private Const SYNCHRONIZE = &H100000

Private Declare Function OpenProcess Lib "kernel32" _
  (ByVal dwDesiredAccess As Long, _
   ByVal bInheritHandle As Long, _
   ByVal dwProcessId As Long) As Long

Private Declare Function WaitForSingleObject Lib "kernel32" _
   (ByVal hHandle As Long, _
   ByVal dwMilliseconds As Long) As Long

Private Declare Function CloseHandle Lib "kernel32" _
  (ByVal hObject As Long) As Long

Public Sub ShellProcess(strProcess As String, Optional blnWait As Boolean = False)
   Dim hProc As Long
   Dim taskId As Long
   Dim cmdline As String

    cmdline = strProcess
    taskId = Shell(cmdline, vbNormalFocus)

    If blnWait = True Then
         hProc = OpenProcess(SYNCHRONIZE, True, taskId)
        Call WaitForSingleObject(hProc, WAIT_INFINITE)
        CloseHandle hProc
    End If

    MsgBox "The shelled app has ended."
End Sub

I have managed to do this in c# sometime ago but now have only vb6 to work with.

Fred
  • 5,663
  • 4
  • 45
  • 74
  • http://stackoverflow.com/questions/5685972/how-to-wait-for-a-shell-process-to-finish-before-executing-further-code-in-vb6 – rags Sep 05 '13 at 09:33
  • @rags I may be missing something but that article doesnt explain how to determine when all child processes of the process shelled have exited. – Fred Sep 05 '13 at 09:57
  • There is [a win32 api ](http://msdn.microsoft.com/en-us/library/windows/desktop/ms686837(v=vs.85).aspx) to enumerate processes. It is possible to find childs (by testing ParentID), and even to do this recursively. However, this will work only if the snapshot is done while all processes are running. As soon as some parents end, the ancestry links are broken and enumeration will not be complete. So knowing the pattern of creation/ending of the childs is mandatory. – d-stroyer Sep 05 '13 at 10:00
  • The link provided has solution for your problem... just try it once – rags Sep 05 '13 at 10:18
  • Try this code: http://www.freevbcode.com/ShowCode.asp?ID=99 – rags Sep 05 '13 at 10:44
  • @rags This example only waits for the shelled process to exit. The situation I have is that I am launching an application (App 1) but then that application launches another (App 2) then exits. I need to wait for the second (App 2) to exit. The code in the link you have posted returns control to my vb app once App 1 exits and ignores App 2. Thanks anyway – Fred Sep 05 '13 at 11:02
  • @Fred, I tried this example with two batch files ... VB invokes batch1 and batch1 invokes batch2. After batch2 is completed, batch1 gets back the control and once batch1 is completed, control goes back to VB application. Is this not the scenario you are trying ? – rags Sep 05 '13 at 11:18
  • @Fred: You can easily monitor all children processes with [Win32 Job Objects](http://msdn.microsoft.com/en-us/library/windows/desktop/ms684161(v=vs.85).aspx) – wqw Sep 06 '13 at 11:37

0 Answers0