HOWTO: Determine When a Shelled Process Has TerminatedLast reviewed: October 13, 1997Article ID: Q96844 |
The information in this article applies to:
SUMMARYExecuting 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. NOTE: The GetModuleUsage() function does not exist in Windows NT. There is a completely different process that would be used to accomplish the same thing from a 32-bit application. For additional information on the 32-bit implementation, please see the following article in the Microsoft Knowledge Base:
ARTICLE-ID: Q129796 TITLE : 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:
Debug.Print Shell("EDIT.COM") Debug.Print Shell("EDIT.COM") Debug.Print Shell("EDIT.COM")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
Keywords : APrgOther vb416 VB4WIN kbfasttip Version : WINDOWS:2.0,3.0,\4.0 Platform : WINDOWS Issue type : kbhowto |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |