|
|
Title | Make a program start another program by using the Shell command and wait until it finishes |
Description | This example shows how to make a program start another program by using the Shell command and wait until it finishes in Visual Basic 6. It starts the other program using Shell, uses the process ID to get a process handle, and then uses the WaitForSingleObject API function to wait until the program finishes. |
Keywords | Shell, run, execute, wait |
Categories | Software Engineering |
|
|
This example starts the other program using Shell, uses the process ID to get a process handle, and then uses the WaitForSingleObject API function to wait until the program finishes.
|
|
' 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
Me.SetFocus
Exit Sub
ShellError:
MsgBox "Error starting task " & _
txtProgram.Text & vbCrLf & _
Err.Description
End Sub
|
|
|
|
|
|