Waiting until a part of the process ended

Posted on 2003-03-25
Medium Priority
Last Modified: 2010-05-01

I've got a little problem with my program.  I use the shell-command and until my shell-command isn't ended my program has to wait with executing the rest of the subprocedure.  Can someone provide me with some easy code.

Can someone also tell me what value I get if my shell-command ends?

Question by:Pingolin
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

Jonyv earned 500 total points
ID: 8201641
Use this code in your form.

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 Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Const SYNCHRONIZE As Long = &H100000

Private Sub WaitForProcess(PID As Long)

Dim hProcess As Long

    hProcess = OpenProcess(SYNCHRONIZE, 0, PID)
    If hProcess <> 0 Then CloseHandle hProcess
    Sleep (100)
Loop While hProcess <> 0

End Sub

Call WaitForProcess with the return value from the Shell function, this will wait until the shelled command has finished.
LVL 70

Expert Comment

by:Éric Moreau
ID: 8201947
Use this:

Option Explicit

Private Declare Function WaitForSingleObject _
                 Lib "kernel32" _
                 (ByVal hHandle As Long, _
                 ByVal dwMilliseconds As Long) As Long
Private Declare Function OpenProcess _
                 Lib "kernel32" _
                 (ByVal dwDesiredAccess As Long, _
                 ByVal bInheritHandle As Long, _
                 ByVal dwProcessId As Long) As Long

Private Const INFINITE = &HFFFF

Private Sub Command1_Click()
    WaitForProcessToEnd "CALC.EXE"
End Sub

Private Function WaitForProcessToEnd(ByVal pstrExecute As String) As Boolean
'*** Execute a process an wait for it to end before continuing here ***
Dim lngHandle As Long
Dim lngProcessID As Long
Dim lngReturnValue As Long

    On Error GoTo ErrorHandler
    WaitForProcessToEnd = False
    lngProcessID = Shell(pstrExecute)
    lngHandle = OpenProcess(&H100000, True, lngProcessID)
    lngReturnValue = WaitForSingleObject(lngHandle, INFINITE)
    MsgBox "Here is the proof that I was waiting!!!"
    WaitForProcessToEnd = True
    Exit Function
    Select Case Err.Number
        Case 53  'File not found
            MsgBox pstrExecute & " cannot be found.", vbOKOnly + vbCritical
            Resume GoHere
        Case Else
            MsgBox Err.Number & " : " & Err.Description
            Resume GoHere
    End Select
End Function


Expert Comment

ID: 8202012
Well, WaitForSingleObject works fine too, but the drawback is that the VB application may look as if it's hanged since it's windows aren't repainted during the wait, that's why I personally prefer the polling method used in my example. But you may of course use whatever method you like.

Author Comment

ID: 8202163
Thanx, that was just wat i wanted.  Now my program is completed!

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question