I need to wait for a system command to complete before processing continues.
Is there a way to do this?
yyes; if I can find my example snippet.
Larry
you'll need this:
type SHELLEXECUTEINFO,4
def cbSize:int
def fMask:int
def hwnd:int
def lpVerb:pointer
def lpFile:pointer
def lpParameters:pointer
def lpDirectory:pointer
def nShow:int
def hInstApp:int
def lpIDList:int
def lpClass:pointer
def hkeyClass:INT
def dwHotKey:int
def hIcon:int
def hProcess:int
endtype
CONST WAITTIMEOUT = 258
CONST SEE_MASK_NOCLOSEPROCESS = 0x40
def info:SHELLEXECUTEINFO
def ret:int
And here is the routine where I used it. I was running through a list of datafile names and running an exe with the file as a command line prarameter.
I had to wait to finsih the running of the exe before I could run it again.
sub process()
int x,ret
string itm$,para$
info.cbSize = 15 * 4
info.fMask = SEE_MASK_NOCLOSEPROCESS
info.hwnd = 0
info.lpVerb = "Open"
info.lpFile = ss_exe
info.lpDirectory = ""
info.nShow = @SWHIDE
info.hInstApp = 0
For x =1 to CONTROLCMD(w1, w1_LV,@LVGETCOUNT)
if GetCheckState(w1, w1_LV,x-1)=1
CONTROLCMD(w1, w1_LV, @LVGETTEXT,x-1,0, itm$)
para$=" /MX "+ss_sdf+itm$+".sdf"
info.lpParameters =para$
ShellExecuteExA(info)
wait 1
do
ret = WaitForSingleObject(info.hprocess,0)
until ret <> WAITTIMEOUT
endif
next x
return
endsub
Hope that gives you an idea.
Larry
Kool - thanks
There is a missing CloseHandle:
do
ret = WaitForSingleObject(info.hprocess, 0)
until ret <> WAITTIMEOUT
CloseHandle(info.hprocess)
CloseHandle is very important if your program is running 24/7
By the way, WaitForSingleObject with NULL timeout does not really wait, it just checks the object state and returns immediately, so this do-until loop will eat all CPU resources.
Maybe the 'wait' (without 1) should be inside do-loop, so your UI will respond to user input while waiting for process termination.
This version will just wait, if the process will run for more than 5 seconds, your programm will be marked as non responding
ShellExecuteExA(info)
WaitForSingleObject(info.hprocess, INFINITE)
CloseHandle(info.hprocess)
This version will wait and respond to user input:
ShellExecuteExA(info)
WHILE WaitForSingleObject(info.hprocess, 1) = WAIT_TIMEOUT
WAIT
ENDWHILE
CloseHandle(info.hprocess)
There is a special api function MsgWaitForMultipleObjects that is capable to return when the object is signalled, a mouse/keyboard/system message is pending. It requires more code, but (summary) is working same as the version with WAIT.
Thanks for fixing my code for me Sapero. :)
It works much better now ;D
Larry