Solved

Waiting for a task to end

Posted on 2000-04-20
16
477 Views
Last Modified: 2008-02-26
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?
0
Comment
Question by:hirschhouse
  • 7
  • 6
  • 2
  • +1
16 Comments
 
LVL 2

Expert Comment

by:Sage020999
ID: 2735524
Use the findwindow API to see if the app is running:

If FindWindow(vbNullString, "PROGNAME") = 0 Then
        Exit Do
End If
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2735592
try inserting DoEvents command after opening windiff
0
 

Author Comment

by:hirschhouse
ID: 2735594
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
 
LVL 28

Expert Comment

by:AzraSound
ID: 2735623
0
 
LVL 2

Expert Comment

by:Sage020999
ID: 2735658
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
 
LVL 3

Expert Comment

by:y2kwacko
ID: 2735669
0
 

Author Comment

by:hirschhouse
ID: 2735900
I must be a real DUMB A** 'cause i can't make the example Azra or y2k gave me work????

any other suggestions?
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2735930
did you put all the needed code in a module?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 28

Expert Comment

by:AzraSound
ID: 2735937
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
 
LVL 28

Expert Comment

by:AzraSound
ID: 2735942
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
 

Author Comment

by:hirschhouse
ID: 2736005
Ummm??? Where does the Declaration go???
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 200 total points
ID: 2736022
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
 

Author Comment

by:hirschhouse
ID: 2736061
Adjusted points from 100 to 200
0
 

Author Comment

by:hirschhouse
ID: 2736062
AzraSound,

YOU ROCK!
0
 

Author Comment

by:hirschhouse
ID: 2736068
Thanks,
I think you deserve a bit extra for the point value.
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2736075
Youre quite welcome. Thanks for the points.  =)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

910 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now