Search code examples
vbscript

Determine if a program started through my script is still running without using winmgmts://


Short introduction

I have an SSD and I frequently record movies with Fraps. Unfortunately, my SSD is not that big and my secondary harddrive is not fast enough for Fraps to record movies to. I decided to write a script that will start Fraps for me, and then checks if Fraps is done recording, moves the file to my second harddisk and notifies me of this event.

This works great. The problem is that I want to exit the script when I close Fraps. The only way VBScript seems to be able to do it (at least, that's what everyone recommends doing) is by querying the processlist to see if the program is active. Because Fraps is a heavy program, and I run many programs at the same time, just querying this list creates a lag spike in my recording. It only happens if I have too many programs open, but the programs themselves are not the problem, just the amount.

Given that it creates just one lag spike with one check is already too much so I'm really looking for a different solution if there is any.

Visual Basic

I've programmed a lot in Visual Basic, even though it was a many many years ago, and later using VBA, and in there you can basically start a program, retreive the handle of it, and by that handle alone, check if the application still runs. Does something like that exist in VBScript?

If VBScript won't do it, is there another simple macro scripting program that can check for filesize, execute a program and determine if that program is still running or not at a later point in the script?

The code I currently use for determining if a program runs is this:

Function IsProcessRunning( strComputer, strProcess )
    Dim Process, strObject
    IsProcessRunning = False
    strObject   = "winmgmts://" & strComputer
    For Each Process in GetObject( strObject ).InstancesOf( "win32_process" )
        If UCase( Process.name ) = UCase( strProcess ) Then
            IsProcessRunning = True
            Exit Function
        End If
    Next
End Function

Solution

  • I found out how to do it using the method I used to do in Visual Basic, but using the new things I learned during researching Rich's method.

    Even though Rich's method doesn't work, it pushed me into the right direction, so I'm giving him a vote up.

    Here's the script for starting Fraps:

    Dim oShell, oFraps
    Set oShell = WScript.CreateObject( "WScript.Shell" )
    set oFraps = oShell.exec("""c:\path\to\fraps\fraps.exe""") 
    

    Here's the main script that keeps looping until Fraps has exited.

    do while IsProcessRunning()
        'your code here
    loop
    wscript.quit
    

    And here's the function to see if Fraps still runs.

    Function IsProcessRunning()
        dim result
        result = oFraps.StdOut.readall
    end function
    

    I just wanted to see if the above thrown in an error and work with an error catcher to do it, but I was very surprised when just this code made my script work! It starts fraps for me, and the script continues to run until I close fraps. I use sapi.speak to notify me when the script starts and ends, and I can keep fraps open for a long time and I don't hear that the script ends. I then close fraps, and moments later the script tells me that it ends.