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

Timeout a VBScript?

Status
Not open for further replies.

humbletech99

Programmer
Nov 22, 2005
155
0
0
GB
Is there a way to set a maximum time on execution of a vbscript from within the vbscript itself?

Eg. in some languages you can set an alarm function near the beginning which terminates the script with a custom message if execution of the rest of the script takes longer than X seconds.

This means that if a later part of the script was to hang, the alarm mechanism started near the beginning would kill the remaining execution and exit gracefully with a message.

Any ideas on a timeout of execution within a vbscript? Perhaps some COM method? I'm not sure if I'm close with something like System.Windows.Forms.Timer or if I'm way off.

In any event I need it to be portable and not rely on installing the .NET framework. It has to come bundled with all modern windows versions, 2000/2003/XP...
 
You may use the //T command line option.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Sure, like this. This script will only run for 1 minute.

Code:
startTime = Now[green]
'H = Hours
'N = Minutes
'S = Seconds[/green]
endTime = DateAdd("N",1,startTime)

Do Until Now >= endTime
	WScript.Echo Now
Loop

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
You can timeout a vbscript from the cscript command using /T.
Code:
cscript //t:10 loopforever.vbs
will run loopforever.vbs for 10s then terminate with
Script execution time was exceeded on script "C:\loopforever.vbs".
Script execution was terminated.
 
PHV and xwb: ah, thanks I had forgotten about that option. I was actually looking for a programmatic way of doing this though. One reason being, I can't rely on a user to always run a script with the //T:x option and I need the script to contain a default timeout if non is specified. I don't want the command line to get any longer either really as vbs is inconvenient enough having to type "cscript scriptname options..."

Mark: That is not exactly what I mean. I don't want to delay execution of anything, I want it to proceed but be killed if it takes longer than X seconds.

If you were to do what you have shown above, you would have to put all of your code within the loop and what would happen if something in the loop where to hang for some unknown reason like blocking IO? The loop would never cycle because it would never reason the end of the iteration and the loop test would never force the termination of the script. Also, I want to run the whole thing just once in a time constrained manner, no loop in this case...

 
Just put this at the top of your script.

Code:
Dim oShell
Set oShell = CreateObject("Wscript.Shell")

forceUseCScript

Sub forceUseCScript()
   If Not WScript.FullName = WScript.Path & "\cscript.exe" Then
      oShell.Popup "Launched using wscript. Relaunching...",3,"WSCRIPT"
      oShell.Run "cmd.exe /k " & WScript.Path & "\cscript.exe //NOLOGO //T:120000 " & Chr(34) & WScript.scriptFullName & Chr(34),1,False
      WScript.Quit 0
   End If
End Sub

The above function will allow your user to just double click the script. It will then relaunch under CSCRIPT and execute your //T parameter.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Thanks Mark, that is not dissimilar to what I do to force the initial use of cscript, however, I cannot have the script start a new window on this occasion, instead I actually print an error popup and exit from this script.

this means that i can't use the relaunch trick to get the timeout...

I am now using WScript.Timeout = X in the script, which works well enough, but I need to control the return code and message and this doesn't seem to allow me to do that, although I wouldn't know if it did, since it doesn't seem to be documented at Microsoft.com that I can see...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top