Contenido

# Servicio ScriptForge.Timer

El servicio Timer mide el intervalo de tiempo necesario para ejecutar secuencias de órdenes de usuario.

A Timer measures durations. It can be:

- Started, to indicate when to start measuring time.

- Suspended, to pause measuring running time.

- Reanudado, para seguir registrando el tiempo de ejecución después de que el Timer se haya suspendido.

- Restarted, which will cancel previous measurements and start the Timer at zero.

Durations are expressed in seconds with a precision of 3 decimal digits (milliseconds). A duration value of 12.345 means 12 seconds and 345 milliseconds

## Invocación del servicio

Antes de utilizar el servicio Timer, es necesario cargar o importar la biblioteca ScriptForge:

• Para cargar la biblioteca ScriptForge que necesitan las macros de Basic se debe usar la siguiente declaración:
GlobalScope.BasicLibraries.loadLibrary("ScriptForge")
• Los scripts de Python necesitan importar el módulo scriptforge:
from scriptforge import CreateScriptService

##### En BASIC

The example below creates a Timer object named myTimer and starts it immediately.

        GlobalScope.BasicLibraries.LoadLibrary("ScriptForge")
        Dim myTimer As Variant
        myTimer = CreateScriptService("Timer", True)
        'The timer starts immediately when the second argument = True, default = False

Es recomendable liberar recursos después del uso:

        Set myTimer = myTimer.Dispose()

##### En Python

        from scriptforge import CreateScriptService
        myTimer = CreateScriptService("Timer", start = True)
        # ...
        myTimer = myTimer.Dispose()

## Propiedades

Nombre | De solo lectura | Tipo | Descripción
--- | --- | --- | ---
Duration | Sí | Double | The actual running time elapsed since start or between start and stop (does not consider suspended time)
IsStarted | Sí | Boolean | True when timer is started or suspended
IsSuspended | Sí | Boolean | True when timer is started and suspended
SuspendDuration | Sí | Double | The actual time elapsed while suspended since start or between start and stop
TotalDuration | Sí | Double | The actual time elapsed since start or between start and stop (including suspensions and running time)

Note that the TotalDuration property is equivalent to summing the Duration and SuspendDuration properties.

## Métodos

All methods do not require arguments and return a Boolean value.

If the returned value is False, then nothing happened.

Nombre | Descripción | Valor devuelto
--- | --- | ---
Continue | Resumes the Timer if it has been suspended | False if the timer is not suspended
Restart | Terminates the Timer and discards its current property values, restarting as a new clean Timer | False if the timer is inactive
Start | Inicia un temporizador nuevo | False if the timer is already started
Suspend | Suspende un temporizador en ejecución | False if the timer is not started or already suspended
Terminate | Detiene un temporizador en ejecución | False if the timer is neither started nor suspended

### Ejemplo:

Los siguientes ejemplos en BASIC y Python ilustran el uso de los métodos y las propiedades de servicio Timer.

##### En BASIC

        myTimer.Start()
        Wait 500
        myTimer.Suspend()
        'El tiempo transcurrido mientras el diálogo esté abierto se contará como tiempo de suspensión
        MsgBox myTimer.Duration & " " & myTimer.SuspendDuration & " " & myTimer.TotalDuration
        myTimer.Continue()
        Wait 500
        'El tiempo transcurrido mientras el diálogo esté abierto se contará como tiempo de funcionamiento
        MsgBox myTimer.Duration & " " & myTimer.SuspendDuration & " " & myTimer.TotalDuration
        myTimer.Terminate()
        'Muestra las mediciones de tiempo finales
        MsgBox myTimer.Duration & " " & myTimer.SuspendDuration & " " & myTimer.TotalDuration

If you call the Terminate method, subsequent calls for the Continue method will not resume time measurement. Similarly, after a Timer has been terminated, calling the Start method will restart it as if it were a clean new Timer.

##### En Python

        from time import sleep
        bas = CreateScriptService("Basic")
        myTimer.Start()
        sleep(0.5)
        myTimer.Suspend()
        bas.MsgBox("{} {} {}".format(myTimer.Duration, myTimer.SuspendDuration, myTimer.TotalDuration))
        myTimer.Continue()
        sleep(0.5)
        bas.MsgBox("{} {} {}".format(myTimer.Duration, myTimer.SuspendDuration, myTimer.TotalDuration))
        myTimer.Terminate()
        bas.MsgBox("{} {} {}".format(myTimer.Duration, myTimer.SuspendDuration, myTimer.TotalDuration))

Debe saber que la función Wait de BASIC recibe argumentos de duración en milisegundos, a diferencia de la función sleep de Python, que utiliza segundos en su argumento.

## Working with Multiple Timers

It is possible to instantiate multiple Timer services in parallel, which gives flexibility in measuring time in different parts of the code.

The following example illustrates how to create two Timer objects and start them separately.

##### En BASIC

        Dim myTimerA as Variant, myTimerB as Variant
        myTimerA = CreateScriptService("Timer")
        myTimerB = CreateScriptService("Timer")
        'Starts myTimerA
        myTimerA.Start()
        Wait 1000 'Wait 1 second (1,000 milliseconds)
        MsgBox myTimerA.Duration & " " & myTimerB.Duration
        'Starts myTimerB
        myTimerB.Start()
        Wait 1000
        MsgBox myTimerA.Duration & " " & myTimerB.Duration
        'Terminate both timers
        myTimerA.Terminate()
        myTimerB.Terminate()

##### En Python

        from time import sleep
        myTimerA = CreateScriptService("Timer")
        myTimerB = CreateScriptService("Timer")
        myTimerA.Start()
        sleep(1)
        bas.MsgBox("{} {}".format(myTimerA.Duration, myTimerB.Duration))
        myTimerB.Start()
        sleep(1)
        bas.MsgBox("{} {}".format(myTimerA.Duration, myTimerB.Duration))
        myTimerA.Terminate()
        myTimerB.Terminate()

Todas las rutinas o identificadores BASIC de ScriptForge precedidas por guion bajo «_» están reservadas para uso interno. No deben utilizarse en macros BASIC o secuencias Python.

Temas relacionados

Función Now

Función Timer

¡Necesitamos su ayuda!