Solved

Waiting for a task to end

Posted on 2000-04-20
16
485 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

623 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