Results 1 to 3 of 3

Thread: Shell Ending

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2001
    Location
    portugal
    Posts
    5

    Arrow Shell Ending

    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?

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    You could use the WaitForSingleObjectEx() API after getting the Process Handle from the returned ProcessID using the OpenProcess() API, i.e.

    In a Module:
    VB Code:
    1. Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    2. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    3. Private Declare Function WaitForSingleObjectEx Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long, ByVal bAlertable As Long) As Long
    4.  
    5. Private Const PROCESS_ALL_ACCESS = &H1F0FFF
    6.  
    7. Public Function RunEXE(ByVal Filename As String) As Single
    8.     Dim lProcessID As Long
    9.     Dim lProcessHandle As Long
    10.     Dim tTimer As Single
    11.     Dim bStillRunning As Boolean
    12.    
    13.     tTimer = Timer
    14.     lProcessID = Shell(Filename, vbNormalFocus)
    15.     lProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, 0, lProcessID)
    16.     Do
    17.         bStillRunning = WaitForSingleObjectEx(lProcessHandle, 10, 0)
    18.         DoEvents
    19.     Loop While bStillRunning
    20.     Call CloseHandle(lProcessHandle)
    21.    
    22.     RunEXE = (Timer - tTimer)
    23. End Function
    Example:
    VB Code:
    1. Private Sub Command1_Click()
    2.     MsgBox "Notepad was closed after running for: " & Round(RunEXE("Notepad.exe"), 2) & " seconds.", vbInformation + vbOKOnly, "Time Running:"
    3. End Sub

  3. #3
    Hyperactive Member Jason Badon's Avatar
    Join Date
    Feb 2001
    Location
    Colorado
    Posts
    329
    Thanks for the code Aaron is very useful.

    Jason

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width