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

VB6 Shell and wait hanging my application window

Status
Not open for further replies.

tjbradford

Technical User
Dec 14, 2007
229
GB
Below is some code to shell an app and wait for it to finish.
is there a way that it can shell and wait but still allow you to move the form around , it just gives a hanging effect which is not disirable.


Option Explicit

Const SYNCHRONIZE = &H100000
Const INFINITE = &HFFFF 'Wait forever
Const WAIT_OBJECT_0 = 0 'The state of the specified object is signaled.
Const WAIT_TIMEOUT = &H102 'The time-out interval elapsed, and the
'object’s state is nonsignaled.

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


If Trim$(Text5) = "" Then Exit Sub

lPid = Shell(Text5.Text, vbHide)
If lPid <> 0 Then
lHnd = OpenProcess(SYNCHRONIZE, 0, lPid) 'Get a handle to the shelled process.
If lHnd <> 0 Then 'If successful, wait for the
lRet = WaitForSingleObject(lHnd, 8000) ' application to end.
CloseHandle (lHnd) 'Close the handle.
End If

MsgBox "Just terminated.", vbInformation, "Shelled Application"
End If
End Sub
 
Replace

lRet = WaitForSingleObject(lHnd, 8000)

with

Do Until lRet = WAIT_OBJECT_0
lRet = WaitForSingleObject(lHnd, 10)
DoEvents
Loop

as a cheap and cheerful solution. And see thread222-827392 for a little discussion on whether the use of DoEvents is called for here (and a hint at my preferred solution for this involving MsgWaitForMultipleObjects)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top