x 分钟后终止 vbscript [英] Terminate vbscript after x minutes
问题描述
我正在使用 vbscript 编写脚本,我希望它在 x 分钟后自行终止.
I am working on a script with vbscript, and I would like it to terminate itself after x number of minutes.
我在想一些事情,比如在脚本开始时抓住时间,然后将整个事情保持在一个循环中,直到时间是开始时间后的 x 分钟,但我需要它继续在后台检查,而不是只需等到循环完成即可.
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?
推荐答案
按照 Ekkehard.Horner 可能是您的最佳选择.另一种略有不同的方法可能如下所示:
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.
这篇关于x 分钟后终止 vbscript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!