ACC: How to Determine When a Shelled Process EndsLast reviewed: January 5, 1998Article ID: Q178116 |
The information in this article applies to:
SUMMARYAdvanced: Requires expert coding, interoperability, and multiuser skills. When you run the Shell() function in a Visual Basic for Applications procedure, it starts an executable program asynchronously and returns control to the procedure. This shelled program continues to run independently of your procedure until you close it. If your procedure needs to wait for the shelled process to end, you can use the Windows API to poll the status of the application, but this is not very efficient. This article demonstrates a more efficient method. This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.
MORE INFORMATIONThe Win32 API has integrated functionality that enables your application to wait until a shelled process has completed. To use these functions, you need to have a handle to the shelled process. To accomplish this, you need to use the CreateProcess() function instead of the Shell() function to begin your shelled program.
Creating the Shelled ProcessTo create an addressable process, use the CreateProcess() function to start your shelled application. The CreateProcess() function gives your program the process handle of the shelled process via one of its passed parameters.
Waiting for the Shelled Process to EndOnce you use the CreateProcess() function to get a process handle, you can pass that handle to the WaitForSingleObject() function. This causes your Visual Basic for Applications procedure to suspend execution until the shelled process ends. Below are the steps necessary to build a Visual Basic for Applications procedure that uses the CreateProcess() function to run the Windows Notepad (NOTEPAD.EXE) application. This code shows by example how to use the Windows API CreateProcess() and WaitForSingleObject() functions to wait until a shelled process ends before resuming execution. The syntax of the CreateProcess() function is complex, so in the example code, it is encapsulated into a function called ExecCmd(). ExecCmd() takes one parameter, the command line of the application to execute.
Step-by-Step Instructions on How to Create the CreateProcess() Procedure
REFERENCESFor more information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:
ARTICLE-ID: Q163435 TITLE : VBA: Programming Resources for Visual Basic for Applications |
Additional query words: vba
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |