PDA

View Full Version : Shell() and Process Waiting in VB6


obiwanjabroni
06-15-2004, 08:45 PM
Hi. To start off, I'm working on a program in visual basic and I need to access an outside component (a low level, compiled program) from within visual basic. The outside program generates some files that are used by the visual basic client in an immediate section of code directly after the call from VB. However, the shell command runs a program "asynchroneously", which is a clever microsoft jargon for at the same time as all your other code, so it's not really a sequential part. I'm wondering if its possible to make VB wait until the program is finished executing? Maybe a method for checking if the PID of the other program is still in the process table?

Any help would be welcome. Thanks.

scroots
06-15-2004, 08:53 PM
if you modify your code to do something like this

Call Function


See if output files exist

false: wait and try again

true: proceed

read files
Delete files.


By doing that dleeting them after reading or looking at the date created would let you know if the program supplying the output files has done its task.

scroots

obiwanjabroni
06-15-2004, 09:00 PM
Hmm, I've thought of that but I'm not sure how to do a check on existance. I could try opening the files and if it's immediately at the end of file condition, make the program wait and try again. And should I be using a timer for this method? Sorry, I'm much better at C++.

sage45
06-16-2004, 02:19 AM
Here is a wait module I used:

modWait:Option Explicit

Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Private Const WAIT_ABANDONED& = &H80&
Private Const WAIT_ABANDONED_0& = &H80&
Private Const WAIT_FAILED& = -1&
Private Const WAIT_IO_COMPLETION& = &HC0&
Private Const WAIT_OBJECT_0& = 0
Private Const WAIT_OBJECT_1& = 1
Private Const WAIT_TIMEOUT& = &H102&

Private Const INFINITE = &HFFFF
Private Const ERROR_ALREADY_EXISTS = 183&

Private Const QS_HOTKEY& = &H80
Private Const QS_KEY& = &H1
Private Const QS_MOUSEBUTTON& = &H4
Private Const QS_MOUSEMOVE& = &H2
Private Const QS_PAINT& = &H20
Private Const QS_POSTMESSAGE& = &H8
Private Const QS_SENDMESSAGE& = &H40
Private Const QS_TIMER& = &H10
Private Const QS_MOUSE& = (QS_MOUSEMOVE _
Or QS_MOUSEBUTTON)
Private Const QS_INPUT& = (QS_MOUSE _
Or QS_KEY)
Private Const QS_ALLEVENTS& = (QS_INPUT _
Or QS_POSTMESSAGE _
Or QS_TIMER _
Or QS_PAINT _
Or QS_HOTKEY)
Private Const QS_ALLINPUT& = (QS_SENDMESSAGE _
Or QS_PAINT _
Or QS_TIMER _
Or QS_POSTMESSAGE _
Or QS_MOUSEBUTTON _
Or QS_MOUSEMOVE _
Or QS_HOTKEY _
Or QS_KEY)

Private Declare Function CreateWaitableTimer Lib "kernel32" _
Alias "CreateWaitableTimerA" ( _
ByVal lpSemaphoreAttributes As Long, _
ByVal bManualReset As Long, _
ByVal lpName As String) As Long

Private Declare Function OpenWaitableTimer Lib "kernel32" _
Alias "OpenWaitableTimerA" ( _
ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal lpName As String) As Long

Private Declare Function SetWaitableTimer Lib "kernel32" ( _
ByVal hTimer As Long, _
lpDueTime As FILETIME, _
ByVal lPeriod As Long, _
ByVal pfnCompletionRoutine As Long, _
ByVal lpArgToCompletionRoutine As Long, _
ByVal fResume As Long) As Long

Private Declare Function CancelWaitableTimer Lib "kernel32" ( _
ByVal hTimer As Long)

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

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

Private Declare Function MsgWaitForMultipleObjects Lib "user32" ( _
ByVal nCount As Long, _
pHandles As Long, _
ByVal fWaitAll As Long, _
ByVal dwMilliseconds As Long, _
ByVal dwWakeMask As Long) As Long

Public Sub Wait(lNumberOfSeconds As Long)
Dim ft As FILETIME
Dim lBusy As Long
Dim lRet As Long
Dim dblDelay As Double
Dim dblDelayLow As Double
Dim dblUnits As Double
Dim hTimer As Long

hTimer = CreateWaitableTimer(0, True, App.EXEName & "Timer")

If Err.LastDllError = ERROR_ALREADY_EXISTS Then
' If the timer already exists, it does not hurt to open it
' as long as the person who is trying to open it has the
' proper access rights.
Else
ft.dwLowDateTime = -1
ft.dwHighDateTime = -1
lRet = SetWaitableTimer(hTimer, ft, 0, 0, 0, 0)
End If

' Convert the Units to nanoseconds.
dblUnits = CDbl(&H10000) * CDbl(&H10000)
dblDelay = CDbl(lNumberOfSeconds) * 1000 * 10000

' By setting the high/low time to a negative number, it tells
' the Wait (in SetWaitableTimer) to use an offset time as
' opposed to a hardcoded time. If it were positive, it would
' try to convert the value to GMT.
ft.dwHighDateTime = -CLng(dblDelay / dblUnits) - 1
dblDelayLow = -dblUnits * (dblDelay / dblUnits - _
Fix(dblDelay / dblUnits))

If dblDelayLow < CDbl(&H80000000) Then
' &H80000000 is MAX_LONG, so you are just making sure
' that you don't overflow when you try to stick it into
' the FILETIME structure.
dblDelayLow = dblUnits + dblDelayLow
ft.dwHighDateTime = ft.dwHighDateTime + 1
End If

ft.dwLowDateTime = CLng(dblDelayLow)
lRet = SetWaitableTimer(hTimer, ft, 0, 0, 0, False)

Do
' QS_ALLINPUT means that MsgWaitForMultipleObjects will
' return every time the thread in which it is running gets
' a message. If you wanted to handle messages in here you could,
' but by calling Doevents you are letting DefWindowProc
' do its normal windows message handling---Like DDE, etc.
lBusy = MsgWaitForMultipleObjects(1, hTimer, False, _
INFINITE, QS_ALLINPUT&)
DoEvents
Loop Until lBusy = WAIT_OBJECT_0

' Close the handles when you are done with them.
CloseHandle hTimer

End Sub

To call use the following in your program:
Wait X 'X is the number of seconds

As for the function to check for the existance of the output file... If you know it's location, you can do this:

Dim objFSO As Object

Public Sub FileCHK()
On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
If Not objFSO.FileExists("C:\myprogdir\output.txt") Then
MsgBox "Missing C:\myprogdir\output.txt.", vbExclamation, "Critical Error!"
Wait 20 ' seconds
FileCHK
Else
' Next Sub Function here.
End If
End Sub

HTH,

-sage-

Roelf
06-16-2004, 11:50 AM
declare the following variables and functions in your application


Private Const WAIT_INFINITE = -1&
Private Const SYNCHRONIZE = &H100000

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


now you can call an shell-app synchroneously like the following
strExecute holds the commandline, the app waits until the called application stops

Private Sub Command1_Click()
Dim strExecute As String
strExecute = "cmd /c dir c:\ /a:-d /s /b > d:\list.txt"


Dim hProcess As Long
Dim taskId As Long

taskId = Shell(strExecute, vbHide)

hProcess = OpenProcess(SYNCHRONIZE, True, taskId)
Call WaitForSingleObject(hProcess, WAIT_INFINITE)
CloseHandle hProcess

MsgBox "The shelled app has ended."
End Sub

obiwanjabroni
06-18-2004, 09:21 PM
Wow, hey thanks for all the responses! Sorry I sound so clueless, but I essentially inherited the work on some old VB code (you should see this monster, it's a mess for what needs to be done). Anyway, thanks, I'll be sure to use your suggestions (I'll be trying both, just to get a sense of the runtimes of each).