uvCrq‖ScriptForge.Timer service

WyVvH‖The Timer service measures the amount of time it takes to run user scripts.

qDa8E‖A Timer measures durations. It can be:

Tip Icon

dm7yA‖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


CVhDR‖Service invocation

fYto9‖Before using the Timer service the ScriptForge library needs to be loaded or imported:

note

gF8D8‖• Basic macros require to load ScriptForge library using the following statement:
GlobalScope.BasicLibraries.loadLibrary("ScriptForge")

• Python scripts require an import from scriptforge module:
from scriptforge import CreateScriptService


3aa4B‖In Basic

SCYEX‖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)
    WMZog‖'The timer starts immediately when the second argument = True, default = False
  

CnZqc‖It is recommended to free resources after use:


    Set myTimer = myTimer.Dispose()
  
BenDd‖In Python

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

8h3fp‖Properties

dVncX‖Name

hFnkK‖Readonly

NvqK9‖Type

7zFYh‖Description

Duration

T92or‖Yes

Double

9yDgM‖The actual running time elapsed since start or between start and stop (does not consider suspended time)

IsStarted

ThAaG‖Yes

Boolean

tqpDU‖True when timer is started or suspended

IsSuspended

pSPgk‖Yes

Boolean

SGyi4‖True when timer is started and suspended

SuspendDuration

qoNpD‖Yes

Double

E45MD‖The actual time elapsed while suspended since start or between start and stop

TotalDuration

gxF8S‖Yes

Double

FeCob‖The actual time elapsed since start or between start and stop (including suspensions and running time)


Tip Icon

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


Mav4g‖Methods

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

onEib‖If the returned value is False, then nothing happened.

U82Do‖Name

6oGwx‖Description

ZMfpe‖Returned value

Continue

6DJTP‖Resumes the Timer if it has been suspended

ixF7A‖False if the timer is not suspended

Restart

AAozF‖Terminates the Timer and discards its current property values, restarting as a new clean Timer

UtCTT‖False if the timer is inactive

Start

AkgAy‖Starts a new clean timer

B4gTh‖False if the timer is already started

Suspend

D7CoH‖Suspends a running timer

YbeSJ‖False if the timer is not started or already suspended

Terminate

sgXra‖Stops a running timer

WkCCC‖False if the timer is neither started nor suspended


EFSA4‖Example:

DuD3h‖The examples below in Basic and Python illustrate the use of the methods and properties in the Timer service.

3aa4B‖In Basic

    myTimer.Start()
    Wait 500
    myTimer.Suspend()
    UgBnC‖'The time elapsed while the Dialog box is open will be counted as suspended time
    MsgBox myTimer.Duration & " " & myTimer.SuspendDuration & " " & myTimer.TotalDuration
    myTimer.Continue()
    Wait 500
    4jHcj‖'The time elapsed while the Dialog box is open will be counted as running time
    MsgBox myTimer.Duration & " " & myTimer.SuspendDuration & " " & myTimer.TotalDuration
    myTimer.Terminate()
    7QhZU‖'Shows the final time measurements
    MsgBox myTimer.Duration & " " & myTimer.SuspendDuration & " " & myTimer.TotalDuration
  
note

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


BenDd‖In 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))
  
note

AqGfb‖Be aware that the Wait function in BASIC takes in a duration argument in milliseconds whereas the sleep function in Python uses seconds in its argument.


bHEyr‖Working with Multiple Timers

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

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

3aa4B‖In Basic

    Dim myTimerA as Variant, myTimerB as Variant
    myTimerA = CreateScriptService("Timer")
    myTimerB = CreateScriptService("Timer")
    PtA4E‖'Starts myTimerA
    myTimerA.Start()
    Wait 1000 'Wait 1 second (1,000 milliseconds)
    MsgBox myTimerA.Duration & " " & myTimerB.Duration
    VUdGW‖'Starts myTimerB
    myTimerB.Start()
    Wait 1000
    MsgBox myTimerA.Duration & " " & myTimerB.Duration
    t98Fv‖'Terminate both timers
    myTimerA.Terminate()
    myTimerB.Terminate()
  
BenDd‖In 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()
  
warning

uzETY‖All ScriptForge Basic routines or identifiers that are prefixed with an underscore character "_" are reserved for internal use. They are not meant be used in Basic macros or Python scripts.