The information in this article applies to:
SUMMARY
Executing the Shell() function in a Visual Basic for Windows program starts
another executable program asynchronously and returns control to the Visual
Basic application. The shelled program continues to run indefinitely until
the user closes it -- not until your Visual Basic program terminates.
However, your program can wait until the shelled program has finished by
polling the return value of the Windows API GetModuleUsage() function. This
article describes the method and provides a code example. Q129796 How to Determine When a Shelled 32-bit Process Has Terminated MORE INFORMATIONThis information is included with the Help file provided with Microsoft Visual Basic version 3.0 for Windows, and on the Microsoft Developer Network (MSDN) Visual Basic Starter Kit provided with Microsoft Visual Basic version 4.0, Professional and Enterprise Editions. Technique Also Works with MS-DOS ProgramsThe technique described in this article also works for MS-DOS programs. The return value from the Visual Basic Shell() function is a unique instance handle to the MS-DOS session that was started in the Shell(). If you call GetModuleUsage() with that handle after the MS-DOS session in question has ended, GetModuleUsage() will return 0 because the handle is no longer valid. This can be verified with the following code:
Executing this code will show that the Shell() return value is unique for
each of the shelled MS-DOS programs. Using GetModuleUsage() on one of these
handles after the associated EDIT.COM program has been terminated will
return zero (because the handle isn't valid anymore) and take you out of
the wait loop.
Monitoring the Status of a Shelled ProcessBy using the Windows API GetModuleUsage() function, your Visual Basic program can monitor the status of a shelled process. The return value from the Shell() function can be used to call the GetModuleUsage() function continuously within a loop to find out if the shelled program has finished. If the Shell() function is successful, the return value is the instance handle for the shelled program. This instance handle can be passed to the GetModuleUsage() function to determine the reference count for the module. When the GetModuleUsage() function returns a value of 0 or less, the shelled program has finished.This algorithm works correctly regardless of the WindowStyle used to shell the program. In addition, this method works correctly when:
Step-by-Step Example
Additional query words:
Keywords : kb16bitonly kbVBp400 VB4WIN |
Last Reviewed: September 17, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |