Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Timeout a VBScript?

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,54353
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Timeout a VBScript?
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 Timeout a VBScript? - 12/16/2007 5:24:47 AM   
  humbletech99

 

Posts: 53
Score: 0
Joined: 6/9/2007
Status: offline
]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...

< Message edited by humbletech99 -- 12/16/2007 7:42:09 AM >


_____________________________

value(geeks) > value(mundanes)
 
 
Post #: 1
 
 RE: Timeout a VBScript? - 12/16/2007 6:48:18 AM   
  dm_4ever


Posts: 2663
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline

      

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to humbletech99)
 
 
Post #: 2
 
 RE: Timeout a VBScript? - 12/16/2007 7:31:14 AM   
  humbletech99

 

Posts: 53
Score: 0
Joined: 6/9/2007
Status: offline
thank you so much, that is a very succinct and good answer and it works very well.

One thought occurs, what is the exit code when the timeout forces a closure and can I set this or handle it to set it myself somehow? I expect that an On Error Resume Next isn't going to catch this timeout killer?

< Message edited by humbletech99 -- 12/16/2007 7:41:42 AM >


_____________________________

value(geeks) > value(mundanes)

(in reply to dm_4ever)
 
 
Post #: 3
 
 RE: Timeout a VBScript? - 12/16/2007 2:23:48 PM   
  dm_4ever


Posts: 2663
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
I don't believe there is a specific exit code for that and may simply return a 0.

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to humbletech99)
 
 
Post #: 4
 
 RE: Timeout a VBScript? - 12/16/2007 11:12:15 PM   
  humbletech99

 

Posts: 53
Score: 0
Joined: 6/9/2007
Status: offline
that is actually a big problem for me, I am writing something to must return the right exit codes for it to be useful...

_____________________________

value(geeks) > value(mundanes)

(in reply to dm_4ever)
 
 
Post #: 5
 
 RE: Timeout a VBScript? - 12/16/2007 11:18:00 PM   
  ehvbs

 

Posts: 2196
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi humbletech99,

can't you return an ErrorLevel distinct from the one returned in case of
timeout?

(in reply to humbletech99)
 
 
Post #: 6
 
 RE: Timeout a VBScript? - 12/16/2007 11:20:38 PM   
  humbletech99

 

Posts: 53
Score: 0
Joined: 6/9/2007
Status: offline
The question is how to do that? Wouldn't it require another thread, I'm not sure vbs has this...

I cannot do sleep or stuff like that, I need the rest of the script to be doing it's own thing instead.... and then for all of that to get killed with a custom exit code and preferably message too.

_____________________________

value(geeks) > value(mundanes)

(in reply to ehvbs)
 
 
Post #: 7
 
 RE: Timeout a VBScript? - 12/16/2007 11:25:12 PM   
  ehvbs

 

Posts: 2196
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Assuming the script returns 0 in case of timeout, make sure a full run
terminates with WScript.Quit 42 (or something less funny).

(in reply to humbletech99)
 
 
Post #: 8
 
 RE: Timeout a VBScript? - 12/16/2007 11:36:22 PM   
  humbletech99

 

Posts: 53
Score: 0
Joined: 6/9/2007
Status: offline
but this is the problem for me, 0 indicates success, which it must not if terminated prematurely via timeout it must return something else that I choose. I know how to set the exit code with wscript.quit otherwise, but a timeout will not call this, hence I either need to figure out how to set the exit code for the timeout or I need to not use the timeout, but I really want to be able to time out this script...

_____________________________

value(geeks) > value(mundanes)

(in reply to ehvbs)
 
 
Post #: 9
 
 RE: Timeout a VBScript? - 12/16/2007 11:44:14 PM   
  ehvbs

 

Posts: 2196
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Then wrap the script in a .bat or .vbs that maps the exit code of called script from 42 to 0 and
from 0 to 42.

(in reply to humbletech99)
 
 
Post #: 10
 
 RE: Timeout a VBScript? - 12/17/2007 1:08:38 AM   
  humbletech99

 

Posts: 53
Score: 0
Joined: 6/9/2007
Status: offline
that is way too ugly a hack....

_____________________________

value(geeks) > value(mundanes)

(in reply to ehvbs)
 
 
Post #: 11
 
 RE: Timeout a VBScript? - 12/17/2007 7:30:53 AM   
  ziminski

 

Posts: 79
Score: 2
Joined: 1/8/2006
Status: offline
what about using the t switch?
cscript //T:nn      'Time out in seconds:  Maximum time a script is permitted to run

(in reply to humbletech99)
 
 
Post #: 12
 
 RE: Timeout a VBScript? - 12/17/2007 7:40:43 AM   
  ebgreen


Posts: 5032
Score: 31
Joined: 7/12/2005
Status: online
I would have to check but I suspect that the //T switch would still cause a 0 return. Assuming that is the case, does your script have some base task that it is repeatedly doing and you are concerned that this repitition will take too long? If that is the case, then you may be able to restructure the loop with a time check. We would need more information on exactly what you are doing to help with that.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to ziminski)
 
 
Post #: 13
 
 RE: Timeout a VBScript? - 12/17/2007 9:02:37 AM   
  humbletech99

 

Posts: 53
Score: 0
Joined: 6/9/2007
Status: offline
no loop, straight through execution.

I believe that //T:nn is the same as WScript.Timeout = nn

_____________________________

value(geeks) > value(mundanes)

(in reply to ebgreen)
 
 
Post #: 14
 
 RE: Timeout a VBScript? - 12/17/2007 9:16:53 AM   
  ebgreen


Posts: 5032
Score: 31
Joined: 7/12/2005
Status: online
Alright, let's back up a bit. Is there one specific activity in you script that causes a delay? Could you post code for us to look at?

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to humbletech99)
 
 
Post #: 15
 
 RE: Timeout a VBScript? - 12/17/2007 9:21:50 AM   
  humbletech99

 

Posts: 53
Score: 0
Joined: 6/9/2007
Status: offline
no this is a general coding guideline for a project I am working on. All code must time itself out to prevent it taking longer than X seconds to run.

My current script is only making 1 wmi call and iterating through the result set, a straight through script really...

I just really really want to know how to do this properly like I can in python... but in vbs since it's a native windows language and more portable across windows systems than python (if python shipped with every windows system then of course python is the superior language and I'd use that, but I need windows portability without the difficulty of compiled code at this point)

_____________________________

value(geeks) > value(mundanes)

(in reply to ebgreen)
 
 
Post #: 16
 
 RE: Timeout a VBScript? - 12/17/2007 9:41:43 AM   
  ebgreen


Posts: 5032
Score: 31
Joined: 7/12/2005
Status: online
"iterating through the result set"

So there is a repeated activity that you can attach a time check to.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to humbletech99)
 
 
Post #: 17
 
 RE: Timeout a VBScript? - 12/17/2007 9:51:37 AM   
  humbletech99

 

Posts: 53
Score: 0
Joined: 6/9/2007
Status: offline
I see what you are getting at but the coding guidelines say that that there must be a coded timeout for the execution of the entire program, not just an iteration loop on a set of records or objects. This is what I am trying to achieve.

_____________________________

value(geeks) > value(mundanes)

(in reply to ebgreen)
 
 
Post #: 18
 
 RE: Timeout a VBScript? - 12/17/2007 10:02:12 AM   
  ebgreen


Posts: 5032
Score: 31
Joined: 7/12/2005
Status: online
Ok, no one has stated it yet, but I will state it for you. There is no built in way to do it in VBScript. The best you can do is to do periodic checks through your code and handle it yourself. So you do something like:


      

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to humbletech99)
 
 
Post #: 19
 
 RE: Timeout a VBScript? - 12/17/2007 10:05:50 AM   
  humbletech99

 

Posts: 53
Score: 0
Joined: 6/9/2007
Status: offline
yes I thought this was the case... I'm not going to do this timeout thing because it just won't be worth it. The whole point is that if a script were to hang at one point, then handling it yourself would be a waste of time.

I suspect I could handle this better if I could get the Pid of the current cscript process and then launch an external application on a timer loop to kill the original cscript... which I think would be the best solution but also probably not worth the trouble it would cause...

_____________________________

value(geeks) > value(mundanes)

(in reply to ebgreen)
 
 
Post #: 20
 
 
Page:   [1] 2   next >   >>
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Timeout a VBScript? Page: [1] 2   next >   >>
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts