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

Wait for a process to finish, before going to another line

Status
Not open for further replies.

cblock1025

Technical User
Jan 31, 2004
31
US
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
 
Hello cblock1025,

[1] To monitor the wfica32.exe process being created as anticipated within a timeout.
Code:
const timeout=3000    'in millisecond
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 timeout=3000    'in millisecond
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
You can can it somehow to suit your concrete need.

regards - tsuji
 
I tried out your code. However, it doesn't want to stay in the for loop. It finds the process so it prompts me with "Some process wfica32.exe is still running within the timeout period, and then just keeps going by exiting. I need to be able to keep this program open till a user clicks the x to close. Then the pc reboots. I will be messing around with your code a bit, to see what I can get. Is there anyway to loop the process? even though I do not like loops going on in the back.

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!!!!!!
 
Well I molded your code and got what i wanted. Thanks again!!!!


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
 
cblock1025,

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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top