Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pausing an app until an external program finishes

Status
Not open for further replies.

topher2798

Technical User
May 24, 2002
7
0
0
US
I'm using VB6 and I have a sub procedure that calls an external application. I need my VB app to automatically close AFTER the external application is finished and not before. How would I go about doing this?

Here is what I am working with:

Private Sub Command1_Click()
Shell App.Path & ".\bin\snark.exe"
End
End Sub

The problem with this is that it executes the snark.exe and then continues down to the next line and ends the VB app. I need it to wait until snark.exe is completly finished running before it runs the next line of code.

Any help on this one?
 
The WaitForSingleObject API may prove useful in this context.

Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessID As Long) As Long

Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Const SYNCHRONIZE = &H100000
Const WAIT_TIMEOUT = &H102

Public Sub ShellAndWait(CommandLine As String)

Dim dwProcessID As Long
Dim hProcess As Long

dwProcessID = Shell(CommandLine, vbNormalFocus)

If dwProcessID <> 0 Then
hProcess = OpenProcess(SYNCHRONIZE, False, dwProcessID)
If hProcess <> 0 Then
Do While WaitForSingleObject(hProcess, 100) = WAIT_TIMEOUT
DoEvents
Loop
CloseHandle hProcess
End If
End If

End Sub
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein


 
I had a situation similar to the one you describe. The external application writes to a file. The main application simply keeps trying to rename the file. Once it is able to do so it continues. (The rename fails whilst the external application has the file open).

Originally the retry logic was in a loop including a DoEvents, but I subsequently changed this to use a timer in order to reduce the CPU usage.

Glyn.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top