Terminate vbscript after x minutes

time, vbscript

Solution

Re-launching the script with `//T:xx` as suggested by Ekkehard.Horner is probably your best option. Another, slightly different, approach could look like this:

Const Timeout = 4 'minutes

timedOut = False

If WScript.Arguments.Named.Exists("relaunch") Then
  'your code here
Else
  limit = DateAdd("n", Timeout, Now)
  cmd = "wscript.exe """ & WScript.ScriptFullName & """ /relaunch"
  Set p = CreateObject("WScript.Shell").Exec(cmd)
  Do While p.Status = 0
    If Now < limit Then
      WScript.Sleep 100
    Else
      On Error Resume Next  'to ignore "invalid window handle" errors
      p.Terminate
      On Error Goto 0
      timedOut = True
    End If
  Loop
End If

If timedOut Then WScript.Echo "Script timed out."

You'd still be re-launching the script, but in this case it's your script killing the child process, not the script interpreter.

Problem

I am working on a script with vbscript, and I would like it to terminate itself after x number of minutes. I was thinking something like grabbing the time when the script starts and then keeping the whole thing in a loop until the time is x number of minutes after the start time, but I need it to keep checking in the background, and not just wait until a loop is complete. I want a message or something that notifies the user they took too long, which I can do myself. Is there any way to keep track of the time in the background, or will it be a bit of a drawn-out process to determine it?

Original source