|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Visual Basic FAQ WaitForSingleObject: Determine when a Process has Ended |
||
Posted: | Thursday December 26, 1996 | |
Updated: | Monday December 26, 2011 | |
Applies to: | VB4-32, VB5, VB6 | |
Related: |
GetExitCodeProcess: Determine when a Shelled App has Ended WaitForSingleObject: Determine when a Process has Ended WaitForSingleObject: Determine when a Shelled App has Ended |
|
Prerequisites |
None. |
|
In a Visual Basic application, once a call to the CreateProcess API has been made, program execution continues on immediately, just as is the case with the Shell() command. When its necessary to wait for the started application to finish before your app continues you need to call WaitForSingleObject. |
BAS Module Code |
None. |
|
Form Code |
Add a command button to a form along with the following code: |
|
Option Explicit '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved. ' Some pages may also contain other copyrights by the author. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Distribution: You can freely use this code in your own ' applications, but you may not reproduce ' or publish this code on any web site, ' online service, or distribute as source ' on any media without express permission. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Const NORMAL_PRIORITY_CLASS = &H20& Private Const WAIT_INFINITE= -1& Private Type STARTUPINFO cb As Long lpReserved As String lpDesktop As String lpTitle As String dwX As Long dwY As Long dwXSize As Long dwYSize As Long dwXCountChars As Long dwYCountChars As Long dwFillAttribute As Long dwFlags As Long wShowWindow As Long cbReserved2 As Long lpReserved2 As Long hStdInput As Long hStdOutput As Long hStdError As Long End Type Private Type PROCESS_INFORMATION hProcess As Long hThread As Long dwProcessId As Long dwThreadID As Long End Type Private Declare Function CreateProcess Lib "kernel32" _ Alias "CreateProcessA" _ (ByVal lpAppName As Long, _ ByVal lpCommandLine As String, _ ByVal lpProcessAttributes As Long, _ ByVal lpThreadAttributes As Long, _ ByVal bInheritHandles As Long, _ ByVal dwCreationFlags As Long, _ ByVal lpEnvironment As Long, _ ByVal lpCurrentDirectory As Long, _ lpStartupInfo As STARTUPINFO, _ lpProcessInformation As PROCESS_INFORMATION) As Long Private Declare Function WaitForSingleObject Lib "kernel32" _ (ByVal hHandle As Long, _ ByVal dwMilliseconds As Long) As Long Private Declare Function CloseHandle Lib "kernel32" _ (ByVal hObject As Long) As Long Private Sub Command1_Click() RunProcess "c:\windows\notepad.exe" End Sub Private Sub RunProcess (cmdline As String) Dim proc As PROCESS_INFORMATION Dim start As STARTUPINFO 'Initialize the STARTUPINFO structure by 'passing to start the size of the STARTUPINFO 'type. Setting the .cb member is the only 'item of the structure needed to launch the program start.cb = Len(start) 'Start the application Call CreateProcess(0&, cmdline, 0&, 0&, 1&, _ NORMAL_PRIORITY_CLASS, 0&, 0&, _ start, proc) 'Wait for the application to finish Call WaitForSingleObject(proc.hProcess, WAIT_INFINITE) 'Close the handle to the process Call CloseHandle(proc.hProcess) 'Close the handle to the thread created Call CloseHandle(proc.hThread) MsgBox "The Shelled process " & cmdline & " has ended." End Sub |
Comments |
Run the app, and click the command button. Notepad will launch, and when closed, the message box will appear. |
|
|
|
|||||
|
|||||
|
|||||
Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved. |