Wait for a Process to Terminate w/ WaitForSingleObject

Very often the need arises to start an application using the Shell command and wait for it to end before continuing with your processing. It is very easy to do this using the WaitForSingleObject API call. The following code and discussion details how to determine when a process ends. To see how to wait for multiple items see my Spy on a Folder to Detect When it Changes example. You can also use the Windows Scripting Host (WSH) to wait for a process to terminate.

Wait for Shelled App to Terminate
Download Source Code

Form Code

   Const SYNCHRONIZE = &H100000
   ' Wait forever
   ' The state of the specified object is signaled
   Const WAIT_OBJECT_0 = 0
   ' The time-out interval elapsed & the object’s state is not signaled
   Const WAIT_TIMEOUT = &H102

   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

Copy the following code to the command button's Click event:

   ' The WaitForSingleObject function returns when one of the following occurs:
   ' - The specified object is in the signaled state.
   ' - The time-out interval elapses.
   ' The dwMilliseconds parameter specifies the time-out interval, in milliseconds.
   ' The function returns if the interval elapses, even if the object’s state is
   ' nonsignaled. If dwMilliseconds is zero, the function tests the object’s state
   ' and returns immediately. If dwMilliseconds is INFINITE, the function’s time-out 
   ' interval never elapses.
   ' This example waits an INFINITE amount of time for the process to end. As a
   ' result this process will be frozen until the shelled process terminates. The
   ' down side is that if the shelled process hangs, so will this one.
   ' A better approach is to wait a specific amount of time. Once the time-out
   ' interval expires, test the return value. If it is WAIT_TIMEOUT, the process
   ' is still not signaled. Then you can either wait again or continue with your
   ' processing.
   ' DOS Applications:
   ' Waiting for a DOS application is tricky because the DOS window never goes
   ' away when the application is done. To get around this, prefix the app that
   ' you are shelling to with "command.com /c".
   ' For example: lPid = Shell("command.com /c " & txtApp.Text, vbNormalFocus)
   ' To get the return code from the DOS app, see the attached text file.
   Dim lPid As Long
   Dim lHnd As Long
   Dim lRet As Long

   If Trim$(txtApp) = "" Then Exit Sub

   lPid = Shell(txtApp.Text, vbNormalFocus)

   If lPid <> 0 Then

      'Get a handle to the shelled process.
      lHnd = OpenProcess(SYNCHRONIZE, 0, lPid)
      'If successful, wait for the application to end and close the handle.
      If lHnd <> 0 Then
         lRet = WaitForSingleObject(lHnd, INFINITE) CloseHandle (lHnd)
      End If

      MsgBox "Just terminated.", vbInformation, "Shelled Application"
   End If


Download the source code and run it. It is more illustrative if you step through the code using the debugger. Enter the full path of the program you want to run and wait for. Calculator is a good example. Click the Start App button and the application, Calculator in this case, will be started. The sample program will wait until you close the calculator. You will then see the "Just Terminated" message.

About TheScarms
About TheScarms

Sample code
version info

If you use this code, please mention "www.TheScarms.com"

Email this page

© Copyright 2016 TheScarms
Goto top of page