I have 2 lines of code:
call shell (another command)
some other command
I do NOT want VB to execute the second line
BEFORE the "shell" id finished.
How can I get that?
Printable View
I have 2 lines of code:
call shell (another command)
some other command
I do NOT want VB to execute the second line
BEFORE the "shell" id finished.
How can I get that?
You could use the WaitForSingleObjectEx() API after getting the Process Handle from the returned ProcessID using the OpenProcess() API, i.e.
In a Module:Example:VB Code:
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Declare Function WaitForSingleObjectEx Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long, ByVal bAlertable As Long) As Long Private Const PROCESS_ALL_ACCESS = &H1F0FFF Public Function RunEXE(ByVal Filename As String) As Single Dim lProcessID As Long Dim lProcessHandle As Long Dim tTimer As Single Dim bStillRunning As Boolean tTimer = Timer lProcessID = Shell(Filename, vbNormalFocus) lProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, 0, lProcessID) Do bStillRunning = WaitForSingleObjectEx(lProcessHandle, 10, 0) DoEvents Loop While bStillRunning Call CloseHandle(lProcessHandle) RunEXE = (Timer - tTimer) End FunctionVB Code:
Private Sub Command1_Click() MsgBox "Notepad was closed after running for: " & Round(RunEXE("Notepad.exe"), 2) & " seconds.", vbInformation + vbOKOnly, "Time Running:" End Sub
Thanks for the code Aaron is very useful.
Jason