Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_vb thread: Not waiting for Some Shelled programs


Message #1 by Walt Morgan <wmorgan@s...> on Thu, 13 Jun 2002 13:40:27 -0500
Thanks for the help

But this also is not working..!!!
> This is a multi-part message in MIME format.

------=_NextPart_000_00A2_01C212DF.E0F11C60
Content-Type: text/plain;
	charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable

Tom,

Try the following code, it works for me:

Regards,

Walt

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
=
=3D

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 Const STATUS_PENDING =3D &H103&
Private Const PROCESS_QUERY_INFORMATION =3D &H400

Public Function ShellandWait(ExeFullPath As String, _
Optional TimeOutValue As Long =3D 0) As Boolean
   =20
    Dim lInst As Long
    Dim lStart As Long
    Dim lTimeToQuit As Long
    Dim sExeName As String
    Dim lProcessId As Long
    Dim lExitCode As Long
    Dim bPastMidnight As Boolean
   =20
    On Error GoTo ErrorHandler

    lStart =3D CLng(Timer)
    sExeName =3D ExeFullPath

    'Deal with timeout being reset at Midnight
    If TimeOutValue > 0 Then
        If lStart + TimeOutValue < 86400 Then
            lTimeToQuit =3D lStart + TimeOutValue
        Else
            lTimeToQuit =3D (lStart - 86400) + TimeOutValue
            bPastMidnight =3D True
        End If
    End If

    lInst =3D Shell(sExeName, vbMinimizedNoFocus)
   =20
lProcessId =3D OpenProcess(PROCESS_QUERY_INFORMATION, False, lInst)

    Do
        Call GetExitCodeProcess(lProcessId, lExitCode)
        DoEvents
        If TimeOutValue And Timer > lTimeToQuit Then
            If bPastMidnight Then
                 If Timer < lStart Then Exit Do
            Else
                 Exit Do
            End If
    End If
    Loop While lExitCode =3D STATUS_PENDING
   =20
    ShellandWait =3D True
  =20
ErrorHandler:
ShellandWait =3D False
Exit Function
End Function




------=_NextPart_000_00A2_01C212DF.E0F11C60
Content-Type: text/html;
	charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Dwindows-1252">
<META content=3D"MSHTML 6.00.2600.0" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DTahoma size=3D2>Tom,</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>Try the following code, it works for=20
me:</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>Regards,</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2>Walt</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DTahoma=20
size=3D2>=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
=
=3D=3D=3D=3D</FONT></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV>Private Declare Function OpenProcess Lib "kernel32" _<BR>(ByVal=20
dwDesiredAccess As Long, ByVal bInheritHandle As Long, _<BR>ByVal =
dwProcessId As=20
Long) As Long</DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV>Private Declare Function GetExitCodeProcess Lib "kernel32" =
_<BR>(ByVal=20
hProcess As Long, lpExitCode As Long) As Long</DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV>Private Const STATUS_PENDING =3D &amp;H103&amp;<BR>Private Const=20
PROCESS_QUERY_INFORMATION =3D &amp;H400</DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV>Public Function ShellandWait(ExeFullPath As String, _<BR>Optional=20
TimeOutValue As Long =3D 0) As Boolean<BR>&nbsp;&nbsp;&nbsp;=20
<BR>&nbsp;&nbsp;&nbsp; Dim lInst As Long<BR>&nbsp;&nbsp;&nbsp; Dim =
lStart As=20
Long<BR>&nbsp;&nbsp;&nbsp; Dim lTimeToQuit As Long<BR>&nbsp;&nbsp;&nbsp; =
Dim=20
sExeName As String<BR>&nbsp;&nbsp;&nbsp; Dim lProcessId As=20
Long<BR>&nbsp;&nbsp;&nbsp; Dim lExitCode As Long<BR>&nbsp;&nbsp;&nbsp; =
Dim=20
bPastMidnight As Boolean<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; On =
Error=20
GoTo ErrorHandler</DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV>&nbsp;&nbsp;&nbsp; lStart =3D CLng(Timer)<BR>&nbsp;&nbsp;&nbsp; =
sExeName =3D=20
ExeFullPath</DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV>&nbsp;&nbsp;&nbsp; 'Deal with timeout being reset at=20
Midnight<BR>&nbsp;&nbsp;&nbsp; If TimeOutValue &gt; 0=20
Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If lStart + =
TimeOutValue &lt;=20
86400 =
Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;=20
lTimeToQuit =3D lStart +=20
TimeOutValue<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
Else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;=20
lTimeToQuit =3D (lStart - 86400) +=20
TimeOutValue<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;=20
bPastMidnight =3D True<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End =

If<BR>&nbsp;&nbsp;&nbsp; End If</DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV>&nbsp;&nbsp;&nbsp; lInst =3D Shell(sExeName,=20
vbMinimizedNoFocus)<BR>&nbsp;&nbsp;&nbsp; <BR>lProcessId =3D=20
OpenProcess(PROCESS_QUERY_INFORMATION, False, lInst)</DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV>&nbsp;&nbsp;&nbsp; Do<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =
Call=20
GetExitCodeProcess(lProcessId,=20
lExitCode)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
DoEvents<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If TimeOutValue =
And Timer=20
&gt; lTimeToQuit=20
Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
; If=20
bPastMidnight=20
Then<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
If Timer &lt; lStart Then Exit=20
Do<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =

Else<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
Exit =
Do<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; =

End If<BR>&nbsp;&nbsp;&nbsp; End If<BR>&nbsp;&nbsp;&nbsp; Loop While =
lExitCode =3D=20
STATUS_PENDING<BR>&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp; ShellandWait =
=3D=20
True<BR>&nbsp;&nbsp; <BR>ErrorHandler:<BR>ShellandWait =3D False<BR>Exit =

Function<BR>End Function<BR></DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DTahoma size=3D2></FONT>&nbsp;</DIV></BODY></HTML>

------=_NextPart_000_00A2_01C212DF.E0F11C60--



  Return to Index