Timer Object

Applies to TestComplete 15.70, last modified on December 17, 2024

Description

Using the Timer objects, you can implement the timer functionality in your scripts. Timer objects serve as wrappers over ordinary Windows timers. Use an individual Timer object for each individual timer in your scripts.

The Timer object periodically executes the script routine specified by the Timer.TimerProc property. The execution period is specified in milliseconds by the Interval property. The Timer object also has the Enabled property, which lets you activate or deactivate the timer when necessary.

To create a new timer in scripts, use the Utils.Timers.Add method. This method creates a new Timer object with desired parameters and returns the created object. A reference to this object is stored to the Timers collection, so there is no need to store that reference to a variable. You can obtain the desired Timer object from this collection using the object’s index or name (each Timer object may have a name that makes getting the timer from the collection easier).

To delete Timer objects, use the Delete or Clear methods of the Timers collection.

Members

Remarks

The timer cannot be edited, deleted or disabled inside the timer handler routine. If you try to do this, the TestComplete engine will freeze or even crash. This happens due to restrictions of the internal Timer procedure. These restrictions cannot be removed.

To ensure the correct behavior of the timer, add the following check inside the timer handler routine:

JavaScript, JScript

if (!TimerRoutine)
{
  timerRoutine = true;
}

Python

if not TimerRoutine:
   TimerRoutine = True

VBScript

If Not TimerRoutine Then
   TimerRoutine = True
End If

DelphiScript

if not TimerRoutine then
begin
  TimerRoutine := True;
end;

C++Script, C#Script

if (!TimerRoutine)
{
  timerRoutine = true;
}

Example

The code below demonstrates how you can add a new Timer object to the Timers collection and modify the timer’s properties.

JavaScript, JScript

function TestProc()
{
  // Adds a new object to the Timers collection
  Timer1 = Utils.Timers.Add(10000, "Unit1.TimerRoutine", true);

  // Modifies the timer’s properties
  Timer1.Name = "MyTimer";
  Timer1.Interval = 20000;
  Timer1.TimerProc = "Timers.NewTimerRoutine";
}

function TimerRoutine()
{
  // Specify your code here.
  // This routine will be executed after the specified timeout expires.
}

function NewTimerRoutine()
{
  // Specify your code here.
  // This is a new timer routine.
}

Python

def TestProc():
  # Adds a new object to the Timers collection
  Timer1 = Utils.Timers.Add(10000, "Unit1.TimerRoutine", True)

  # Modifies the timer's properties
  Timer1.Name = "MyTimer"
  Timer1.Interval = 20000
  Timer1.TimerProc = "Timers.NewTimerRoutine"

def TimerRoutine():
  # Specify your code here.
  # This routine will be executed after the specified timeout expires.
  pass


def NewTimerRoutine():
  # Specify your code here.
  # This is a new timer routine.
  pass

VBScript

Sub TestProc
  ' Adds a new object to the Timers collection
  Set Timer1 = Utils.Timers.Add(10000, "Unit1.TimerRoutine", True)

  ' Modifies the timer’s properties
  Timer1.Name = "MyTimer"
  Timer1.Interval = 20000
  Timer1.TimerProc = "Timers.NewTimerRoutine"
End Sub

Sub TimerRoutine
  ' Specify your code here.
  ' This routine will be executed after the specified timeout expires.
End Sub

Sub NewTimerRoutine
  ' Specify your code here.
  ' This is a new timer routine.
End Sub

DelphiScript

procedure TestProc;
begin
  // Adds a new object to the Timers collection
  Timer1 := Utils.Timers.Add(10000, 'Unit1.TimerRoutine', true);

  // Modifies the timer’s properties
  Timer1.Name := 'MyTimer';
  Timer1.Interval := 20000;
  Timer1.TimerProc := 'Timers.NewTimerRoutine';
end;

procedure TimerRoutine;
begin
  // Specify your code here.
  // This routine will be executed after the specified timeout expires.
end;

procedure NewTimerRoutine;
begin
  // Specify your code here.
  // This is a new timer routine.
end;

C++Script, C#Script

function TestProc()
{
  // Adds a new object to the Timers collection
  Timer1 = Utils["Timers"]["Add"](10000, "Unit1.TimerRoutine", true);

  // Modifies the timer’s properties
  Timer1["Name"] = "MyTimer";
  Timer1["Interval"] = 20000;
  Timer1["TimerProc"] = "Timers.NewTimerRoutine";
}

function TimerRoutine()
{
  // Specify your code here.
  // This routine will be executed after the specified timeout expires.
}

function NewTimerRoutine()
{
  // Specify your code here.
  // This is a new timer routine.
}

See Also

Using Timers
Timers Object
Add Method
Clear Method
Delete Method
Items Property

Highlight search results