Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB4/32,VB5,VB6
Expertise: beginner
Jul 15, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Wait for the termination of a program using Windows Scripting Host

If you want to execute an application and waiting for its termination, you can follow two approaches. The "classic" approach is based on API functions such as OpenProcess, WaitForSingleObject and CloseHandle. However, there is a simpler technique based on the Windows Scripting Host object model.

A method exposed by this library lets you to run a file, and specify whether you want to wait for its termination. To use the following code snippet you must add a reference to Windows Scripting Host Object model in the "References " dialog window. The method you're looking for is the Run method of the IWshShell_Class object. Its first argument is the file to run, the second specifies the styles of the application's window and the last is a Boolean value that tells whether you want to wait for the program termination. Here's a function that contains all the necessary code:

Sub RunAndWait(ByVal sFile As String)
    Dim WSHShell As New IWshShell_Class
    WSHShell.Run sFile, , True
End Sub
To test this function, write the following code in your form (it assumes that Notepad is on your system path):

Private Sub Form_Click()
    RunAndWait "notepad.exe"
    MsgBox "Back to your application!"
End Sub
You'll see that the message box will appear only when you close Notepad.
Marco Bellinaso
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date