Wait for a process to finish, before going to another line
Wait for a process to finish, before going to another line
(OP)
I hope there is someone that can help out a bit. I am out of ideas here.
This is the problem:
I have a .exe application, and I need to know when it is finished in order to execute another command, which in my case is a reboot. However, here is the tricky part. My executable actually starts up a run process to connect and then another which is the application itself. First it starts a process called "pn.exe" once it connects(about 3 seconds) it then runs a process called "wfica32.exe". Now, my problem is that I need to make sure the wfica32.exe finishes before executing the following line of code(which is a reboot). Here is how I start the program up.
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "c:\progra~1\citrix\icacli~1\pn.exe /APP
desktop", 1, True
Set WshShell = Nothing
I have tried a wait command, but what happens is that it only thinks the .exe is kicking off only one process, so it doesn´t care about the other process and decides to go to the next line of code.
I have been trying to catch the process with:
set service = GetObject ("winmgmts:")
for each Process in Service.InstancesOf ("Win32_Process")
However, I really don't have an idea, how i can tell it when the process ended (wfica32.exe) to kick the next line of code.
Help would really be a blesing.
Thanks guys
Christian
This is the problem:
I have a .exe application, and I need to know when it is finished in order to execute another command, which in my case is a reboot. However, here is the tricky part. My executable actually starts up a run process to connect and then another which is the application itself. First it starts a process called "pn.exe" once it connects(about 3 seconds) it then runs a process called "wfica32.exe". Now, my problem is that I need to make sure the wfica32.exe finishes before executing the following line of code(which is a reboot). Here is how I start the program up.
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "c:\progra~1\citrix\icacli~1\pn.exe /APP
desktop", 1, True
Set WshShell = Nothing
I have tried a wait command, but what happens is that it only thinks the .exe is kicking off only one process, so it doesn´t care about the other process and decides to go to the next line of code.
I have been trying to catch the process with:
set service = GetObject ("winmgmts:")
for each Process in Service.InstancesOf ("Win32_Process")
However, I really don't have an idea, how i can tell it when the process ended (wfica32.exe) to kick the next line of code.
Help would really be a blesing.
Thanks guys
Christian
RE: Wait for a process to finish, before going to another line
[1] To monitor the wfica32.exe process being created as anticipated within a timeout.
CODE
const timepoll=500 'in millisecond
'assuming if any existing instance of wfica32
'it will not fade out within timeout period
set svc=getobject("winmgmts:root\cimv2")
sQuery="select * from win32_process where name='wfica32.exe'"
set cproc=svc.execquery(sQuery)
iniproc=cproc.count
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "c:\progra~1\citrix\icacli~1\pn.exe /APP
desktop", 1, True
Set WshShell = Nothing
set cproc=nothing
for i=0 to timeout\timepoll
set cproc=svc.execquery(sQuery)
if cproc.count=iniproc+1 then
wscript.echo "The child process wfica32.exe is initiated successfully."
exit for
else
if i=timeout\timepoll then
wscript.echo "The child process wfica32.exe failed to initiated within the timeout period."
else
wscript.sleep timepoll
end if
end if
next
set cproc=nothing
set svc=nothing
[2] Once supposedly wfica32.exe is running somehow, to monitor its dying out, this is how you can do.
CODE
const timepoll=500 'in millisecond
set svc=getobject("winmgmts:root\cimv2")
sQuery="select * from win32_process where name='wfica32.exe'"
set cproc=svc.execquery(sQuery)
iniproc=cproc.count 'it can be more than 1
if iniproc=0 then
wscript.echo "The process wfica32.exe is inexistent. Operation aborted."
set cproc=nothing : set svc=nothing
wscript.quit(1)
end if
set cproc=nothing
for i=0 to timeout\timepoll
set cproc=svc.execquery(sQuery)
if cproc.count=0 then 'this monitor all instances died out
wscript.echo "All processes of wfica32.exe are terminated."
exit for
else
if i=timeout\timepoll then
wscript.echo "Some process wfica32.exe is still running within the timeout period."
else
wscript.sleep timepoll
end if
end if
next
set cproc=nothing
set svc=nothing
regards - tsuji
RE: Wait for a process to finish, before going to another line
Do While Process.Name = wifa32.exe
wscript.sleep = 3000
Loop
Something like that maybe?
I really want to find a good way to do this.
thanks for your quick response!!!!!!
RE: Wait for a process to finish, before going to another line
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "c:\progra~1\citrix\icacli~1\pn.exe /APP desktop", 1, True
Set WshShell = Nothing
Wscript.Sleep 5000
set svc=getobject("winmgmts:root\cimv2")
sQuery="select * from win32_process where name='wfica32.exe'"
set cproc=svc.execquery(sQuery)
iniproc=cproc.count 'it can be more than 1
Do While iniproc = 1
wscript.sleep 5000
set svc=getobject("winmgmts:root\cimv2")
sQuery="select * from win32_process where name='wfica32.exe'"
set cproc=svc.execquery(sQuery)
iniproc=cproc.count
Loop
set cproc=nothing
set svc=nothing
RE: Wait for a process to finish, before going to another line
If you get it up to serve your purpose, that is fine, and that is the simplest form I might also envisage at the beginning.
My original is in a more general setting. You have to make the timeout _long_ and realistic enough. Sure, a simple do loop as what you finally use will work, only from my point of view, I do not know what is wfica32.exe and whether it will end at all and how many instances it might be running when the script runs, so I device the timeout.
- tsuji