Subject: RE: Example Code
*DeclareDeclare Function OpenProcess Lib _
“kernel32” (Byval dwDesiredAccess As Long, _
Byval bInheritHandle As Long, _
Byval dwProcessID As Long) As Long
Declare Function WaitForSingleObject _
Lib “kernel32” (Byval hHandle As Long, _
Byval dwMilliseconds As Long) As Long
Declare Function CloseHandle Lib _
“kernel32” (Byval hObject As Long) As Long
Private Const SYNCHRONIZE = &H100000
Private Const INFINITE = -1&
'Error on call
Private Const WAIT_FAILED = -1&
'Normal completion
Private Const WAIT_OBJECT_0 = 0
'Timeout period elapsed
Private Const WAIT_TIMEOUT = &H102&
*Function
Public Function WaitForProcessClose(Byval PID _
As Long, Byval TimeOut As Long) As Boolean
’ Timeout needs to be passed in milliseconds!
Dim hProc As Long
Dim nRet As Long
Const fdwAccess = SYNCHRONIZE
’ Try opening process; wait on it to close.
hProc = OpenProcess(fdwAccess, False, PID)
If hProc Then
nRet = WaitForSingleObject(hProc, TimeOut)
Select Case nRet
Case WAIT_TIMEOUT
' Still open.
WaitForProcessClose = False
Case WAIT_OBJECT_0
' Process has closed.
WaitForProcessClose = True
Case Else
' Error on call.
WaitForProcessClose = False
’ ApiErrorDump Err.LastDllError, _
’ “WaitForSingleObject”
End Select
Else
' Nothing we can do, at this point.
' Process may be gone already?
’ ApiErrorDump Err.LastDllError, _
’ “OpenProcess”
WaitForProcessClose = True
End If
’ Clean up.
Call CloseHandle(hProc)
End Function
What I also did was to used with the following section in another routine where I had a list of batchfiles to execute, it looks like this:
Dim taskId As Variant
Forall i In BatchFileNameList
Print BatchFileNameList(Listtag(i))
taskid = Shellid(BatchFileNameList(Listtag(i)))
TaskIdList(Listtag(i)) = taskid
End Forall
After this i only need to step thru the list I used anoter function to do this:
Function ExternalCommandsFinished(TaskIdList List As String) As Boolean
ExternalCommandsFinished = False
Forall i In TaskIdList
If WaitForProcessClose(TaskIdList(Listtag(i)),60000) Then
Print TaskIdList(Listtag(i)) & " is finished"
Else
Print TaskIdList(Listtag(i)) & " is hanging"
ExternalCommandsFinished = False
Exit Function
End If
End Forall
ExternalCommandsFinished = True
End Function
To call this function I used:
If ExternalCommandsFinished(TaskIdList) Then
Call whatever
Else
Call whatever
End If