Announcement

Collapse
No announcement yet.

Timeout a VBScript?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Timeout a VBScript?

    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...

  • #2
    Re: Timeout a VBScript?

    I don't think you can do this because it requires a separate thread. Otherwise updating your timer or monitor would starve as execution is blocked by the halted/stalled process.

    VBScript does allow you to create threads but as far as I know there's no easy way to interact with them.
    Andrew

    ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

    Comment


    • #3
      Re: Timeout a VBScript?

      I've found that setting WScript.Timeout is the trick, but unfortunately I don't know how to control the exit code or the exit message which makes it pretty useless to me in fact since I absolutely must have the exit code set if it times out.

      Edit: I'd be very interested in how to use threads in vbs though... I thought and have heard that vbs is not multi threaded...
      Last edited by humbletech99; 17th December 2007, 22:44.

      Comment


      • #4
        Re: Timeout a VBScript?

        WScript.Timeout end the script just with exitcode 0.

        I don't know much about your script, but maybe you can use a time-out on a process/service event monitor? <--> from within the script write a Temp.vbs. Execute Temp.vbs hidden, with the wscript.fullname as script argument.
        This is the monitor that waits for a certain event of a certain process or service be be occour. You can set a time-out for the monitoring part of the script.
        When the monitor in Temp.vbs timed-out, the main vbs will be terminated by the temp.vbs and you can quit temp.vbs with a raised exitcode. When the monitor did registrate the event, then Temp.vbs quits w/ exitcode 0.


        \Rems



        I've found that setting WScript.Timeout is the trick, but unfortunately I don't know how to control the exit code or the exit message
        There are several ways to set a time-out on a script.
        By command line,
        wscript //T:60 c:\somescript.vbs

        to make it permanent for all scripts;
        wscript //T:99 //S --> sets script timeouts to 99 seconds
        wscript //T:0 //S --> turns script timeouts off

        Manually for all scripts
        Start | Run: wscript
        change configuration

        In a script
        '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''''''
        'if you don't click OK within 10 seconds
        'the whole script will timeout...
        wscript.timeout = 10 : MsgBox "hello..."

        wscript.timeout = 0
        'this one is not subject to any timeout,,,
        MsgBox "goodbye..."
        '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''''''''''''''''''''


        :::::::::::::::::::::::Tool:::::::::::::::::::::::
        ::Determine the exitcode of a script:
        @echo off
        wscript "c:\somescript.vbs"
        echo wscript returned %errorlevel%
        if errorlevel 1 goto error
        echo We have success
        goto end
        :error
        echo We have an error
        :end
        pause


        \Rems

        This posting is provided "AS IS" with no warranties, and confers no rights.

        __________________

        ** Remember to give credit where credit's due **
        and leave Reputation Points for meaningful posts

        Comment


        • #5
          Re: Timeout a VBScript?

          Rems, thanks for showing me all the round about ways of setting wscript.timeout, but the problem I am having is not setting the timeout or even finding out the return code.

          The problem is that wscript.timeout is actually not returning the right exit code, it is returning 0 when I need it to return a specific error code if it the script exits from timeout.

          I absolutely must be able to set the exit code and preferably message too of a script timeout in order to be able to use it, otherwise I'll have to remove the timeout altogether as a timeout with exit code 0 is far worse in terms on integration with the other application.

          Comment


          • #6
            Re: Timeout a VBScript?

            All the round about ways of setting wscript.timeout was just for who is interested. It was not meant as an solution.

            The time-out of the monitor, I mensioned in the first part of the reply, is a totally different time-out event then wscript.timeout.
            You can set a time-out in miliseconds on the 'NextEvent Method' of the ExecNotificationQuery Method. (Set wbemObject = wbemEvent.NextEvent(Timeout)). You can monitor defined processes and/or services. And when there are no events during that time, the script ends the monitor and proceed w/ rest of the script. This time-out raises an error number '-2147209215'. You can handle the error and exit with Wscript.quit(err.Number).
            But this was just a long shot.

            \Rems

            This posting is provided "AS IS" with no warranties, and confers no rights.

            __________________

            ** Remember to give credit where credit's due **
            and leave Reputation Points for meaningful posts

            Comment

            Working...
            X