-
Does anyone know of a way of stopping execution of a VB6 program until after a shelled program has been run?
So that if I have something like
Shell "C:\WINDOWS\Desktop\tmp.exe"
Kill "C:\WINDOWS\Desktop\tmp.exe"
I can wait until tmp.exe has stopped running to get rid of it?
-
Start the program using Shell. Wait for it using WaitForSingleObject.
Code:
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
Private Const SYNCHRONIZE = &H100000
Private Const INFINITE = -1&
' Start the indicated program and wait for it
' to finish, hiding while we wait.
Private Sub ShellAndWait(ByVal program_name As String)
Dim process_id As Long
Dim process_handle As Long
' Start the program.
On Error GoTo ShellError
process_id = Shell(program_name, vbNormalFocus)
On Error GoTo 0
' Hide.
Me.Visible = False
DoEvents
' Wait for the program to finish.
' Get the process handle.
process_handle = OpenProcess(SYNCHRONIZE, 0, process_id)
If process_handle <> 0 Then
WaitForSingleObject process_handle, INFINITE
CloseHandle process_handle
End If
' Reappear.
Me.Visible = True
Exit Sub
ShellError:
MsgBox "Error starting task " & _
txtProgram.Text & vbCrLf & _
Err.Description
End Sub
Hope this helps.
Shrog
-
Const STILL_ACTIVE = &H103
Const PROCESS_QUERY_INFORMATION = &H400
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Public Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Public Function gRunProgrammAndWait(sJobToDo As String) As Boolean
Dim hProcess As Long
Dim lRetVal As Long
On Error GoTo err_gRunProgrammAndWait
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, Shell(sJobToDo, 0))
Do
GetExitCodeProcess hProcess, lRetVal
DoEvents
Loop While lRetVal = STILL_ACTIVE
gRunProgrammAndWait = True
Exit Function
err_gRunProgrammAndWait:
gRunProgrammAndWait = False
Exit Function
End Function
Now u can call like this
Private Command1_Click()
if gRunProgramandWait(yourshellcommand) =True then
Msgbox "Task Finished"
end if
end sub
-
Thanks Shrog & faisalkm - greatly appreciated!