Library code snippets

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

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of Wait until a program closes.

Leave a comment

Sign in or Join us (it's free).

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 ...
AddThis

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

We'd love to hear what you think! Submit ideas or give us feedback