Waiting for a task to end

Here we go again!

Here is my code:

Rem to copy from source y times
Dim x As Integer
Dim y As Integer
y = Text1.Text


'
'
For x = 1 To y
    With SHFileOp
        .wFunc = lFileOp
        .pFrom = txtSource.Text & vbNullChar & vbNullChar
        .pTo = txtDestination & vbNullChar & vbNullChar
        .fFlags = lFlags
    End With
lresult = SHFileOperation(SHFileOp)

If cWindiff = 1 Then
'Will launch WINDIFF
    Dim wd As Long
    Dim strCommand As String
    strCommand = "a:\windiff -Sdx a:\d.txt " & txtSource.Text & " " & txtDestination.Text & " "
    wd = Shell(strCommand, vbNormalFocus)
End If

Next x

The problem:

After Windiff launches the loop continues to iterate a new copy process (it begins a new loop) before the Windiff app has time to finish. how do I stop the FOR NEXT long enough to allow Windiff to finish executing?
hirschhouseAsked:
Who is Participating?
 
AzraSoundConnect With a Mentor Commented:
if you change the Public to Private for each function and remove the word Public from the constants they can all just go at the beginning of your form code

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

Private Declare Function GetExitCodeProcess Lib "kernel32" _
    (ByVal hProcess As Long, lpExitCode As Long) As Long

Private Declare Function CloseHandle Lib "kernel32" _
    (ByVal hObject As Long) As Long

Const PROCESS_QUERY_INFORMATION = &H400
Const STATUS_PENDING = &H103&
0
 
Sage020999Commented:
Use the findwindow API to see if the app is running:

If FindWindow(vbNullString, "PROGNAME") = 0 Then
        Exit Do
End If
0
 
AzraSoundCommented:
try inserting DoEvents command after opening windiff
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
hirschhouseAuthor Commented:
Sage,
will your code make the loop wait till the exe is done or will it exit me from the loop. I want the loop to continue after Windiff is through.

0
 
Sage020999Commented:
It will exit when your application is finished.  But AzraSound is correct, I should have put a DoEvents in the loop as well.  I use this logic for a database utility I made that verifies a bunch of Access Databases.  I didn't want 10 instances of the app running so I used the findwindow code and it works pretty well.
0
 
hirschhouseAuthor Commented:
I must be a real DUMB A** 'cause i can't make the example Azra or y2k gave me work????

any other suggestions?
0
 
AzraSoundCommented:
did you put all the needed code in a module?
0
 
AzraSoundCommented:
Try this (once you have made all the needed declarations)

For x = 1 To y
    With SHFileOp
        .wFunc = lFileOp
        .pFrom = txtSource.Text & vbNullChar & vbNullChar
        .pTo = txtDestination & vbNullChar & vbNullChar
        .fFlags = lFlags
    End With
lresult = SHFileOperation(SHFileOp)

If cWindiff = 1 Then
'Will launch WINDIFF
    Dim wd As Long
    Dim strCommand As String
    strCommand = "a:\windiff -Sdx a:\d.txt " & txtSource.Text & " " & txtDestination.Text & " " 
    RunShell strCommand
End If

Next x


Private Sub RunShell (cmdline As String)

    Dim hProcess As Long
    Dim ProcessId As Long
    Dim exitCode As Long

    ProcessId = Shell(cmdline, 1)
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessId)

    Do

        Call GetExitCodeProcess(hProcess, exitCode)
        DoEvents
   
    Loop While exitCode = STATUS_PENDING

    Call CloseHandle(hProcess)

    MsgBox "The shelled process " & cmdline & " has ended."

End Sub

0
 
AzraSoundCommented:
that is a different method from the one I linked you to.  The declarations to put in your module are:

Public Declare Function OpenProcess Lib "kernel32" _
    (ByVal dwDesiredAccess As Long, _
     ByVal bInheritHandle As Long, _
     ByVal dwProcessId As Long) As Long

Public Declare Function GetExitCodeProcess Lib "kernel32" _
    (ByVal hProcess As Long, lpExitCode As Long) As Long

Public Declare Function CloseHandle Lib "kernel32" _
    (ByVal hObject As Long) As Long

Public Const PROCESS_QUERY_INFORMATION = &H400
Public Const STATUS_PENDING = &H103&

0
 
hirschhouseAuthor Commented:
Ummm??? Where does the Declaration go???
0
 
hirschhouseAuthor Commented:
Adjusted points from 100 to 200
0
 
hirschhouseAuthor Commented:
AzraSound,

YOU ROCK!
0
 
hirschhouseAuthor Commented:
Thanks,
I think you deserve a bit extra for the point value.
0
 
AzraSoundCommented:
Youre quite welcome. Thanks for the points.  =)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.