×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

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

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

(OP)
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!!!!!!

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

(OP)
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

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

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close