ScriptForge.Timer service/text/sbasic/shared/03/sf_timer.xhpTimer service
ScriptForge.Timer service
The Timer service measures the amount of time it takes to run user scripts.A Timer measures durations. It can be:Started, to indicate when to start measuring time.Suspended, to pause measuring running time.Resumed, to continue tracking running time after the Timer has been suspended.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
Service invocation
Before using the Timer service the ScriptForge library needs to be loaded or imported:The example below creates a Timer object named myTimer and starts it immediately.GlobalScope.BasicLibraries.LoadLibrary("ScriptForge")Dim myTimer As VariantmyTimer = CreateScriptService("Timer", True)'The timer starts immediately when the second argument = True, default = FalseIt is recommended to free resources after use:Set myTimer = myTimer.Dispose()from scriptforge import CreateScriptServicemyTimer = CreateScriptService("Timer", start = True)# ...myTimer = myTimer.Dispose()
Properties
NameReadonlyTypeDescriptionDurationYesDoubleThe actual running time elapsed since start or between start and stop (does not consider suspended time)IsStartedYesBooleanTrue when timer is started or suspendedIsSuspendedYesBooleanTrue when timer is started and suspendedSuspendDurationYesDoubleThe actual time elapsed while suspended since start or between start and stopTotalDurationYesDoubleThe 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.
Methods
All methods do not require arguments and return a Boolean value.If the returned value is False, then nothing happened.Timer service;ContinueTimer service;RestartTimer service;StartTimer service;SuspendTimer service;Terminate
NameDescriptionReturned valueContinueResumes the Timer if it has been suspendedFalse if the timer is not suspendedRestartTerminates the Timer and discards its current property values, restarting as a new clean TimerFalse if the timer is inactiveStartStarts a new clean timerFalse if the timer is already startedSuspendSuspends a running timerFalse if the timer is not started or already suspendedTerminateStops a running timerFalse if the timer is neither started nor suspended
The examples below in Basic and Python illustrate the use of the methods and properties in the Timer service.myTimer.Start()Wait 500myTimer.Suspend()'The time elapsed while the Dialog box is open will be counted as suspended timeMsgBox myTimer.Duration & " " & myTimer.SuspendDuration & " " & myTimer.TotalDurationmyTimer.Continue()Wait 500'The time elapsed while the Dialog box is open will be counted as running timeMsgBox myTimer.Duration & " " & myTimer.SuspendDuration & " " & myTimer.TotalDurationmyTimer.Terminate()'Shows the final time measurementsMsgBox myTimer.Duration & " " & myTimer.SuspendDuration & " " & myTimer.TotalDurationIf 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.from time import sleepbas = 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))Beware that the Wait function in Basic takes in a duration argument in milliseconds whereas the sleep function in Python uses seconds in its argument.
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.Dim myTimerA as Variant, myTimerB as VariantmyTimerA = CreateScriptService("Timer")myTimerB = CreateScriptService("Timer")'Starts myTimerAmyTimerA.Start()Wait 1000 'Wait 1 second (1,000 milliseconds)MsgBox myTimerA.Duration & " " & myTimerB.Duration'Starts myTimerBmyTimerB.Start()Wait 1000MsgBox myTimerA.Duration & " " & myTimerB.Duration'Terminate both timersmyTimerA.Terminate()myTimerB.Terminate()from time import sleepmyTimerA = 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()