• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 153
  • Last Modified:

Waiting until a part of the process ended

Hello,

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?

Thanx
0
Pingolin
Asked:
Pingolin
  • 2
1 Solution
 
JonyvCommented:
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

Do
    hProcess = OpenProcess(SYNCHRONIZE, 0, PID)
    If hProcess <> 0 Then CloseHandle hProcess
    Sleep (100)
    DoEvents
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.
0
 
Éric MoreauSenior .Net ConsultantCommented:
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
   
GoHere:
    Exit Function
   
ErrorHandler:
    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


0
 
JonyvCommented:
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.
0
 
PingolinAuthor Commented:
Thanx, that was just wat i wanted.  Now my program is completed!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now