?
Solved

Running an external application in a loop

Posted on 2003-03-14
3
Medium Priority
?
132 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
[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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
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…
Suggested Courses
Course of the Month10 days, 4 hours left to enroll

762 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