devxlogo

Wait for the termination of a program using Windows Scripting Host

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, , TrueEnd 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.

See also  Small Business Strategies with Venmo
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist