Solved

Waiting for a task to end

Posted on 2000-04-20
16
476 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
Comment Utility
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
Comment Utility
try inserting DoEvents command after opening windiff
0
 

Author Comment

by:hirschhouse
Comment Utility
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
Comment Utility
0
 
LVL 2

Expert Comment

by:Sage020999
Comment Utility
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
Comment Utility
0
 

Author Comment

by:hirschhouse
Comment Utility
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
Comment Utility
did you put all the needed code in a module?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 28

Expert Comment

by:AzraSound
Comment Utility
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
Comment Utility
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
Comment Utility
Ummm??? Where does the Declaration go???
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 200 total points
Comment Utility
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
Comment Utility
Adjusted points from 100 to 200
0
 

Author Comment

by:hirschhouse
Comment Utility
AzraSound,

YOU ROCK!
0
 

Author Comment

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

Expert Comment

by:AzraSound
Comment Utility
Youre quite welcome. Thanks for the points.  =)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

744 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

12 Experts available now in Live!

Get 1:1 Help Now