Usługa ScriptForge.Timer

Usługa Timer mierzy ilość czasu potrzebnego na uruchomienie skryptów użytkownika.

Timer mierzy czas trwania. To może być:

Ikona wskazówki

Czasy trwania wyrażane są w sekundach z dokładnością do 3 cyfr dziesiętnych (milisekund). Wartość czasu trwania wynosząca 12,345 oznacza 12 sekund i 345 milisekund


Wywoływanie usługi

Przed użyciem usługi Timer należy załadować lub zaimportować bibliotekę ScriptForge:

note

• Podstawowe makra wymagają załadowania biblioteki ScriptForge przy użyciu następującej instrukcji:
GlobalScope.BasicLibraries.loadLibrary("ScriptForge")

• Skrypty Pythona wymagają importu z modułu scriptforge:
from scriptforge import CreateScriptService


W języku Basic

Poniższy przykład tworzy obiekt Timer o nazwie myTimer i natychmiast go uruchamia.


    GlobalScope.BasicLibraries.LoadLibrary("ScriptForge")
    Dim myTimer As Variant
    myTimer = CreateScriptService("Timer", True)
    ' Jeśli drugi argument ma wartość True (domyślnie jest to False), pomiar rozpocznie się natychmiast
  

Zaleca się zwolnienie zasobów po użyciu:


    Set myTimer = myTimer.Dispose()
  
W języku Python

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

Właściwości

Nazwa

Tylko do odczytu

Typ

Opis

Duration

Tak

Double

Rzeczywisty czas pracy, jaki upłynął od rozpoczęcia lub pomiędzy rozpoczęciem a zatrzymaniem (nie uwzględnia czasu zawieszenia)

IsStarted

Yes

Boolean

True when timer is started or suspended

IsSuspended

Yes

Boolean

True when timer is started and suspended

SuspendDuration

Yes

Double

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

TotalDuration

Yes

Double

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


Ikona wskazówki

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


Metody

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

If the returned value is False, then nothing happened.

Nazwa

Opis

Returned value

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

Starts a new clean timer

False if the timer is already started

Suspend

Suspends a running timer

False if the timer is not started or already suspended

Terminate

Stops a running timer

False if the timer is neither started nor suspended


Przykład:

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

W języku Basic

    myTimer.Start()
    Wait 500
    myTimer.Suspend()
    '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
    'The time elapsed while the Dialog box is open will be counted as running time
    MsgBox myTimer.Duration & " " & myTimer.SuspendDuration & " " & myTimer.TotalDuration
    myTimer.Terminate()
    'Shows the final time measurements
    MsgBox myTimer.Duration & " " & myTimer.SuspendDuration & " " & myTimer.TotalDuration
  
note

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.


W języku 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

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.


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.

W języku 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()
  
W języku 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

Wszystkie podstawowe procedury lub identyfikatory ScriptForge poprzedzone znakiem podkreślenia „_” są zarezerwowane do użytku wewnętrznego. Nie należy ich używać w makrach Basic ani skryptach Pythona.