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

Do While Loop whilst a process is running

Status
Not open for further replies.

Pickachu

Programmer
May 17, 2007
7
GB
I am writing a piece of code that checks for a certain process running (in this case it is an upgrade to the MSI engine). I want the code to loop until the process has completed but i'm unsure of how to do this, this is what i have so far;
--------------------------------------------
Option Explicit
Dim objWMIService, objProcess, colProcess
Dim strComputer, strChkProcess
strComputer = "."
strChkProcess = "'KB893803.exe'"

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")

Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strChkProcess )
For Each objProcess in colProcess
' i need some sort of loop on this last part until
' the process is no longer running
--------------------------------------------
Help appreciated
 
I posted this in another site...you should be able to modify it to your needs.

Code:
WaitForProcess ".", "notepad.exe"
WScript.Echo "Notepad is no longer running...continue with script!"

Sub WaitForProcess(strComputer, strProcess)
    Dim wmiQuery : wmiQuery = "Select * From Win32_Process Where Name='" _
                          & strProcess & "'"
    Dim objWMIService : Set objWMIService = GetObject("winmgmts:\\" & strComputer _
                                    & "\root\cimv2")
    Dim colItems : Set colItems = objWMIService.ExecQuery(wmiQuery)

    Dim intProcCount : intProcCount = colItems.Count
    Do While intProcCount > 0
        Set colItems = objWMIService.ExecQuery(wmiQuery)
        intProcCount = colItems.Count
        WScript.Sleep 1000
    Loop
End Sub

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top