Terminate vbscript after x minutes

前端 未结 4 1394
鱼传尺愫
鱼传尺愫 2021-01-14 03:09

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 s

4条回答
  •  渐次进展
    2021-01-14 04:02

    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.

提交回复
热议问题