CodeItBetter Programming Another VB Programming Blog

How to Start another program using Shell and wait until it finishes.

Posted on September 4, 2011
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'System & API - How to Start another program using Shell and wait until it finishes.
'Declarations for Shell Wait Function
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&
 
Public Sub ShellAndWait(ByVal program_name As String, ByVal window_style As VbAppWinStyle)
'This function is used to start a program using Shell and wait until it finishes
Dim process_id As Long
Dim process_handle As Long
 
    ' Start the program.
    On Error GoTo ShellError
    process_id = Shell(program_name, window_style)
    On Error GoTo 0
 
    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
 
ExitHere:
    Exit Sub
 
ShellError:
    RaiseEvent ErrRepo("ShellAndWait", Err.Number, "Error starting task " & Err.Description, False)
End Sub
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.