?
Solved

Running an external application in a loop

Posted on 2003-03-14
3
Medium Priority
?
135 Views
Last Modified: 2010-05-01
I need to be able to run an external application over and over again, each time with the input file updated.  I have attempted this in a loop as follows.

Open NewFilePath For Input As #1
    Do Until EOF(1)
        Open "c:\Program Files\Paradis\Input.dat" For Output As #2
        Input #1, R_ROAD_X1, R_ROAD_Y1, INFO
        Input #1, R_ROAD_X2, R_ROAD_Y2, INFO
        Write #2, R_ROAD_X1
        Write #2, R_ROAD_Y1
        Write #2, R_ROAD_X2
        Write #2, R_ROAD_Y2
        Close #2
        ChDir "c:\program files\paradis"
        Shell "c:\program files\paradis\paradis_calc.exe"
    Loop

This has the problem that when the program loops and it gets back to the execution of the file the input file is still open and being used by the external application.

Is there anyway of waiting until the external application has finished and then looping.

Many thanks for anyones help.

Iain Cowan
0
Comment
Question by:ICowan
3 Comments
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 400 total points
ID: 8135641
add this:

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 Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Const STILL_ACTIVE = &H103
Private Const PROCESS_QUERY_INFORMATION = &H400

Private Sub ShellWait(ByVal JobToDo As String)
Dim hProcess As Long
Dim RetVal As Long
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, Shell(JobToDo, 1))
Do
    GetExitCodeProcess hProcess, RetVal
    DoEvents: Sleep 100
Loop While RetVal = STILL_ACTIVE
End Sub



Then change your code to:

Open NewFilePath For Input As #1
   Do Until EOF(1)
       Open "c:\Program Files\Paradis\Input.dat" For Output As #2
       Input #1, R_ROAD_X1, R_ROAD_Y1, INFO
       Input #1, R_ROAD_X2, R_ROAD_Y2, INFO
       Write #2, R_ROAD_X1
       Write #2, R_ROAD_Y1
       Write #2, R_ROAD_X2
       Write #2, R_ROAD_Y2
       Close #2
       ChDir "c:\program files\paradis"
       ShellWait "c:\program files\paradis\paradis_calc.exe"
       DoEvents
   Loop
0
 
LVL 5

Expert Comment

by:avya2k
ID: 8135814
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Const SYNCHRONIZE = &H100000

Private Sub Exec(Appln as String)
    Dim strAppl As String
    Dim term As Long
    Dim lngTaskId As Long
    Dim lngPHndl As Long
       
    lngTaskId = Shell(Appln, vbNormalFocus)
   
    lngPHndl = OpenProcess(SYNCHRONIZE, 0, lngTaskId)
    If lngPHndl <> 0 Then
        Call WaitForSingleObject(lngPHndl, SYNCHRONIZE)
        Call CloseHandle(lngPHndl)
    End If
    End
End Sub

Private Sub Command1_Click()
  Exec("UR application")
End Sub
0
 

Author Comment

by:ICowan
ID: 8135935
Works absolutely perfectly, many thanks.

Iain
0

Featured Post

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.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
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…
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

621 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