Wait until a program closes

The code below uses a fairly simple method to wait until a program that has been launched (in this case notepad) is closed again. First, it launches notepad, and gets its hWnd, which it can then use to find the task handle. Next, it loops until the WaitForSingleObject API returns a value more than 1, signifying that the program has been closed. Simple!

Option Explicit

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 Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long

Private Const INFINITE = -1&
Private Const SYNCHRONIZE = &H100000

Private pHandle As Long
Private lNotePadhWnd As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_CLOSE = &H10

Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Sub cmdNotepad_Click()
      
    Dim lngTask As Long, lRet As Long, pHandle As Long
    If cmdNotepad.Caption = "Close Notepad" Then
        '// tell notepad to close itself
        SendMessage lNotePadhWnd, WM_CLOSE, 0, 0
        '// this will then get us out of the loop below
    Else
        '// start notepad
        lngTask = Shell("notepad " & App.Path & "wait.txt", vbNormalFocus)  'Start the task
        '// get the window's hWnd
        lNotePadhWnd = GetForegroundWindow
        '// get a handle on the window
        pHandle = OpenProcess(SYNCHRONIZE, False, lngTask)   'Get the task handle
        '// set the status
        lblStatus.Caption = "Waiting for notepad to close..."
        lblStatus.Refresh
        cmdNotepad.Caption = "Close Notepad"
        Do
            '// loop until the program closes
            '// you can pass INFINITE instead of 0 if you wish, and your
            '// program will 'hang' here until the program is finished
            lRet = WaitForSingleObject(pHandle, 0)
            DoEvents
        Loop While lRet <> 0
        '// program closes
        lblStatus.Caption = "Notepad closed."
        cmdNotepad.Caption = "Run Notepad"
    End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
    If cmdNotepad.Caption = "Close Notepad" Then Call cmdNotepad_Click
End Sub

You might also like...

Comments

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Better train people and risk they leave – than do nothing and risk they stay.” - Anonymous