VB Can Determine when a Shelled Process Has Terminated

ID Number: Q72880

1.00

WINDOWS

Summary:

The Shell function initiates a process and returns back to the Visual

Basic program. The process will continue indefinitely until you decide

to stop it. Terminating the Visual Basic program will not cause the

shelled process to terminate; however, you may not want this behavior

if you want the Visual Basic program to wait until the shelled process

has finished before continuing. This article describes a method by

which a Visual Basic program will wait until a shelled process has

terminated.

This information applies to Microsoft Visual Basic programming system

version 1.0 for Windows.

More Information:

By using the Windows API functions GetActiveWindow and IsWindow, your

program can monitor the status of a shelled process. The API function

GetActiveWindow should be called from within a loop after the Shell

function to get the window handle of main application window for the

shelled process. This will work correctly only if you invoke the Shell

function using a window style with focus (that is, window style 1, 2,

or 3) and a main application window is displayed for the shelled

process. This routine will work when shelling to Windows or MS-DOS

programs. It will not work for applications that do not display a

window (such as applications that run invisibly in the background).

By continually calling the API function IsWindow from within a While

loop, you can make the Visual Basic program wait until the shelled

process has terminated. The Windows API function IsWindow simply

checks to make sure that the window associated with the handle found

with GetActiveWindow is still a valid, existing window.

Below are the steps necessary to build a Visual Basic program that

uses the Shell function to execute the Windows Calculator accessory.

The program is an example of how to use the Windows API functions

GetActiveWindow and IsWindow to wait until a shelled process has

terminated before resuming execution.

Code Example

------------

1. Run Visual Basic, or from the File menu, choose New Project (ALT,

F, N) if Visual Basic is already running. Form1 is created by

default.

2. Add the following code to the general Declarations section of Form1:

DefInt A-Z

Declare Function GetActiveWindow Lib "User" ()

Declare Function IsWindow Lib "User" (ByVal hWnd)

Const False = 0

Const True = Not False

3. Add the following code to the Form_Load event procedure of Form1:

Sub Form_Load ()

form1.Show 'Show the form: This will cause the hWnd property

'for Form1 to become valid

TimeOutPeriod = 5 'Maximum seconds to wait for application window

'to appear

r = Shell("calc.exe", 1) 'Must Shell using a Window Style

'with focus, Window style 1, 2, or 3.

'Wait for the application window of the shelled process

'to become active.

fTimeOut = False

s! = Timer

Do

r = DoEvents() 'Process Windows events for the shelled

'application.

'WARNING: Calling DoEvents allows the

'user to select any window. If the user

'selects another window, this routine

'will incorrectly treat that window as

'part of the shelled process.

hWndShelledWindow = GetActiveWindow()

'Set timeout flag if time has expired

If Timer - s! > TimeOutPeriod Then fTimeOut = True

Loop While hWndShelledWindow = form1.hWnd And Not fTimeOut

'If a timeout occurred, display a timeout message and terminate

If fTimeOut Then

MsgBox "Timeout waiting for shelled application", 16

End

End If

While IsWindow(hWndShelledWindow) 'Wait until the shelled

'process has terminated.

r = DoEvents() 'Process pending Windows events

Wend

MsgBox "Shelled application just terminated", 64

End

End Sub

Additional reference words: 1.00