Link to home
Start Free TrialLog in
Avatar of Pingolin
Pingolin

asked on

Waiting until a part of the process ended

Hello,

I've got a little problem with my program.  I use the shell-command and until my shell-command isn't ended my program has to wait with executing the rest of the subprocedure.  Can someone provide me with some easy code.

Can someone also tell me what value I get if my shell-command ends?

Thanx
ASKER CERTIFIED SOLUTION
Avatar of Jonyv
Jonyv

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Éric Moreau
Use this:

Option Explicit

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

Private Const INFINITE = &HFFFF

Private Sub Command1_Click()
    WaitForProcessToEnd "CALC.EXE"
End Sub

Private Function WaitForProcessToEnd(ByVal pstrExecute As String) As Boolean
'**********************************************************************
'*** Execute a process an wait for it to end before continuing here ***
'**********************************************************************
Dim lngHandle As Long
Dim lngProcessID As Long
Dim lngReturnValue As Long

    On Error GoTo ErrorHandler
   
    WaitForProcessToEnd = False
   
    lngProcessID = Shell(pstrExecute)
    lngHandle = OpenProcess(&H100000, True, lngProcessID)
    lngReturnValue = WaitForSingleObject(lngHandle, INFINITE)
   
    MsgBox "Here is the proof that I was waiting!!!"
   
    WaitForProcessToEnd = True
   
GoHere:
    Exit Function
   
ErrorHandler:
    Select Case Err.Number
        Case 53  'File not found
            MsgBox pstrExecute & " cannot be found.", vbOKOnly + vbCritical
            Resume GoHere
        Case Else
            MsgBox Err.Number & " : " & Err.Description
            Resume GoHere
    End Select
End Function


Avatar of Jonyv
Jonyv

Well, WaitForSingleObject works fine too, but the drawback is that the VB application may look as if it's hanged since it's windows aren't repainted during the wait, that's why I personally prefer the polling method used in my example. But you may of course use whatever method you like.
Avatar of Pingolin

ASKER

Thanx, that was just wat i wanted.  Now my program is completed!