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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can you WAIT after a CALL stmt ?? 1

Status
Not open for further replies.

Dylan

MIS
Aug 27, 1998
109
US
I have a call stmt in a pgm that runs a batch file. I would like the pgm to pause until the CALL to this batch file completes due to the fact that the next stmt uses the file created by the batch pgm in an ImportExternalData stmt. Without some kind of pause the program trys to do the import before the fle is created.<br>
<br>
Any help would be appreciated.<br>
<br>
Thanks
 
you programming in VB?<br>
-mike<br>
---<br>
Mike_Lacey@Cargill.Com<br>

 
Yes, This code would be in an Event in my Access 97 db.
 
hmmm....in QB 4.5 you can use:<br>
PAUSE x<br>
(x=numb of secs to pause) to make the program stop for a little while; not sure on a VB counterpart, but you might have a little success if you tried that way.<br>
<br>
<br>
-Robherc<br>
robherc@netzero.net
 
I would use the following steps:<br>
<br>
1. Make sure any old version of the file is deleted.<br>
<br>
2. Set an error trap that pauses briefly.<br>
<br>
3. Try to open the file from within a while-loop.<br>
<br>
4. Exit the while-loop when successfully opened.<br>
<br>
This overcomes the problem with a fixed-length pause, which may be too short (the program fails) or too long (the user gets fed up).
 
Here's a VB5 module that will do the trick. I'm not really an Access person - sorry.<br>
<br>
The ShellAndWaitFor function is the one to call.<br>
<br>
Regards<br>
<br>
Mike<br>
<br>
==snip==<br>
Attribute VB_Name = &quot;ProcessControl&quot;<br>
Option Explicit<br>
<br>
Private Declare Function OpenProcess Lib &quot;kernel32&quot; (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long<br>
Private Declare Function CloseHandle Lib &quot;kernel32&quot; (ByVal hObject As Long) As Long<br>
Private Declare Function WaitForSingleObject Lib &quot;kernel32&quot; (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long<br>
Private Declare Function CreateProcessBynum Lib &quot;kernel32&quot; Alias &quot;CreateProcessA&quot; (ByVal lpApplicationName As String, ByVal lpCommandLine As String, ByVal lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDirectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long<br>
Private Declare Function WaitForInputIdle Lib &quot;user32&quot; (ByVal hProcess As Long, ByVal dwMilliseconds As Long) As Long<br>
Private Declare Function ShellExecute Lib &quot;shell32.dll&quot; Alias &quot;ShellExecuteA&quot; (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long<br>
<br>
<br>
Private Const SYNCHRONIZE = &H100000<br>
Private Const INFINITE = &HFFFF ' Infinite timeout<br>
Private Const DEBUG_PROCESS = &H1<br>
Private Const DEBUG_ONLY_THIS_PROCESS = &H2<br>
Private Const CREATE_SUSPENDED = &H4<br>
Private Const DETACHED_PROCESS = &H8<br>
Private Const CREATE_NEW_CONSOLE = &H10<br>
Private Const NORMAL_PRIORITY_CLASS = &H20<br>
Private Const IDLE_PRIORITY_CLASS = &H40<br>
Private Const HIGH_PRIORITY_CLASS = &H80<br>
Private Const REALTIME_PRIORITY_CLASS = &H100<br>
Private Const CREATE_NEW_PROCESS_GROUP = &H200<br>
Private Const CREATE_NO_WINDOW = &H8000000<br>
Private Const WAIT_FAILED = -1&<br>
Private Const WAIT_OBJECT_0 = 0<br>
Private Const WAIT_ABANDONED = &H80&<br>
Private Const WAIT_ABANDONED_0 = &H80&<br>
Private Const WAIT_TIMEOUT = &H102&<br>
<br>
Private Type PROCESS_INFORMATION<br>
hProcess As Long<br>
hThread As Long<br>
dwProcessId As Long<br>
dwThreadId As Long<br>
End Type<br>
<br>
Private Type STARTUPINFO<br>
cb As Long<br>
lpReserved As String<br>
lpDesktop As String<br>
lpTitle As String<br>
dwX As Long<br>
dwY As Long<br>
dwXSize As Long<br>
dwYSize As Long<br>
dwXCountChars As Long<br>
dwYCountChars As Long<br>
dwFillAttribute As Long<br>
dwFlags As Long<br>
wShowWindow As Integer<br>
cbReserved2 As Integer<br>
lpReserved2 As Long<br>
hStdInput As Long<br>
hStdOutput As Long<br>
hStdError As Long<br>
End Type<br>
<br>
Public Function ShellAndWaitFor(Command As String) As Boolean<br>
<br>
Dim res As Long<br>
Dim RetVal As Boolean<br>
Dim sinfo As STARTUPINFO<br>
Dim pinfo As PROCESS_INFORMATION<br>
<br>
sinfo.cb = Len(sinfo)<br>
sinfo.lpReserved = vbNullString<br>
sinfo.lpDesktop = vbNullString<br>
sinfo.lpTitle = vbNullString<br>
sinfo.dwFlags = 0<br>
<br>
res = CreateProcessBynum(Command, vbNullString, 0, 0, True, NORMAL_PRIORITY_CLASS, _<br>
ByVal 0&, vbNullString, sinfo, pinfo)<br>
<br>
If (res = 1) Then<br>
WaitFor pinfo<br>
RetVal = True<br>
Else<br>
RetVal = False<br>
End If<br>
<br>
ShellAndWaitFor = RetVal<br>
<br>
End Function<br>
<br>
Private Sub WaitFor(pinfo As PROCESS_INFORMATION)<br>
Dim res As Long<br>
<br>
' Let the process initialize<br>
Call WaitForInputIdle(pinfo.hProcess, INFINITE)<br>
' Don't need the thread handle - so lose that straight off<br>
Call CloseHandle(pinfo.hThread)<br>
Do<br>
If WaitForSingleObject(pinfo.hProcess, 0) &lt;&gt; WAIT_TIMEOUT Then Exit Do<br>
DoEvents<br>
Loop While True<br>
<br>
' Kill the last handle of the process<br>
Call CloseHandle(pinfo.hProcess)<br>
<br>
' And relax<br>
<br>
End Sub<br>
==snip==<br>
<br>
<p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= > </a><br>
 
Hmmm.....have the batch program delete/create a diff file after it's done with the first & use a DO WHILE loop to keep testing for the presence/absence of the other file in your VB app before proceeding...that whould take a few less lines of code. <p>-Robherc<br><a href=mailto:robherc@netzero.net>robherc@netzero.net</a><br><a href= > </a><br>*nix installation & program collector/reseller. Contact me if you think you've got one that I don't :)
 
Rob,<br>
<br>
I can understand why you made the comment - looks like a sledge hammer to crack a nut.<br>
<br>
But there are a couple of advantages to doing it this way:<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;Solution is generic. It will work in any application you write and be easy to use.<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;The code is in a single module. This makes it easy to include and to use.<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;It doesn't rely on the ability to create a file.<br>
<br>
Remember - you have to be <b>LIP</b>py - that's Lazy, Impatient and Proud<br>
<br>
<b>Lazy</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Because you never want to write anything twice.<br>
<b>Impatient</b>&nbsp;&nbsp;&nbsp;&nbsp;Because it has to run just that <b>bit</b> quicker.<br>
<b>Proud</b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Because you don't want someone to read your code - in a years time - and say &quot;which (*&% wrote this?&quot;<br>
<br>
<b>&lt;grin&gt;</b><br>
<br>
Mike <p>Mike Lacey<br><a href=mailto:Mike_Lacey@Cargill.Com>Mike_Lacey@Cargill.Com</a><br><a href= > </a><br>
 
Mike's right. Write once and forget about it. It's hard to remember but becomes hard to forget in a loop without a DoEvents.
 
If you take people's code, you could at least give them some credit for writing it, and not say it is you that wrote it...

This is a post on how to launch an application and wait for it to execute before proceding on to the next line of code...


Maybe it's just me, but your code looks exactly like the one on the above link, and that one was submitted 9 days before yours. So next time give the guy some credit and put a link to the original source....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top