rtcomp
asked on
Shell
How do I pause the execution of my program durring a shell() function?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Take a look at CreateProcess instead of Shell and use WaitForSingleObject. I don't know how they work or I'd tell you, but this is pretty much the normal way it is done.
Here is code for ExecCmd. It will spawn child process and wait for it to complete.
'
' These data type declarations are required to support the obscure windows
' function to spawn a child task and wait for it to finish before continuing
'
Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
'
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
'
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) As Long
'
Private Declare Function CreateProcessA Lib "kernel32" ( _
ByVal lpApplicationName As Long, _
ByVal lpCommandLine As String, _
ByVal lpProcessAttributes As Long, _
ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, _
ByVal lpCurrentDirectory As Long, _
lpStartupInfo As STARTUPINFO, _
lpProcessInformation As PROCESS_INFORMATION) As Long
'
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
'
Const NORMAL_PRIORITY_CLASS = &H20&
Const INFINITE = -1&
'
Public Sub ExecCmd(cmdline$)
'
' This executes an external windows program and waits for it to complete
'
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
'
' Initialize the STARTUPINFO structure:
'
start.cb = Len(start)
'
' Start the shelled application
'
ret& = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
'
' Wait for the shelled application to finish:
'
holdhere:
DoEvents
ret& = WaitForSingleObject(proc.h Process, 5000)
If ret& <> 0 Then GoTo holdhere
'
ret& = CloseHandle(proc.hProcess)
End Sub
This, and a lot more, is online on my web page: www.cyberchute.com/rvbus/madmark
M
'
' These data type declarations are required to support the obscure windows
' function to spawn a child task and wait for it to finish before continuing
'
Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
'
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
'
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, _
ByVal dwMilliseconds As Long) As Long
'
Private Declare Function CreateProcessA Lib "kernel32" ( _
ByVal lpApplicationName As Long, _
ByVal lpCommandLine As String, _
ByVal lpProcessAttributes As Long, _
ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, _
ByVal lpCurrentDirectory As Long, _
lpStartupInfo As STARTUPINFO, _
lpProcessInformation As PROCESS_INFORMATION) As Long
'
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
'
Const NORMAL_PRIORITY_CLASS = &H20&
Const INFINITE = -1&
'
Public Sub ExecCmd(cmdline$)
'
' This executes an external windows program and waits for it to complete
'
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
'
' Initialize the STARTUPINFO structure:
'
start.cb = Len(start)
'
' Start the shelled application
'
ret& = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
'
' Wait for the shelled application to finish:
'
holdhere:
DoEvents
ret& = WaitForSingleObject(proc.h
If ret& <> 0 Then GoTo holdhere
'
ret& = CloseHandle(proc.hProcess)
End Sub
This, and a lot more, is online on my web page: www.cyberchute.com/rvbus/madmark
M
ASKER
That is the easiest way.
Thanks
Thanks
Happy to help. :-)