Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Waiting for a task to end

Posted on 2000-04-20
16
Medium Priority
?
486 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
[X]
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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 

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
 
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 800 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
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…
Suggested Courses

721 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