Create a device timer in the Secondtimer table for Setpoint and command timeouts
- Last UpdatedApr 08, 2026
- 3 minute read
This scenario guides you through the steps required to create a device timer for a record in a new telemetered table in the RealTime database (RTDB).
To identify the code associated with each step, see the code comments in the Example code section.
Before you begin
- Open the Program.cs file from the created project.
- Open the RTDB.
- Review the list of additional namespaces needed for the desired scenario.
using System; using System.Runtime.InteropServices; using OASySDNA.RealTime.Data.Common.HighPerformanceDBWrapper; using OASySDNA.RealTime.Data.Common.HighPerformanceDBWrapper.Interfaces; using OASySDNA.RealTime.Data.Common.HighPerformanceSetDBWrapper; using OASySDNA.RealTime.Data.Common.HighPerformanceSetDBWrapper.Interfaces; - Resolve the use of external functions by adding the following code:
[DllImport("libvdb.dll", EntryPoint = "?secondTricre@@YAHHPEADHHH@Z", ExactSpelling = true)] private static extern Int32 secondTricre(Int32 init, String func, Int32 parm, Int32 dataset, Int32 warnPeriod); [DllImport("libvdb.dll", EntryPoint = "?secondTrvdel@@YAXH@Z", ExactSpelling = true)] private static extern void secondTrvdel(Int32 num); - Use the secondTricre method to create a new device timer in the Secondtimer table.
A callback function must be sent to the secondTricre method when it is invoked.In this example, myDLL.dll contains the callback function.
Only extensions written in C++ are supported.
- Make the extension visible to the Secondtimer by adding the extension to the RealTime_extension.json into the TimerCallbacks setting section:
"TimerCallbacks": { myDLL.dll: true },
To create a device timer in the Secondtimer table
The new telemetered table used in this scenario is myTable, which contains the fields timerslot(secondtimer slot#) and timeout(expire command time in seconds) needed in this example.
- Access the myTable table.
- Lock myTable table for reading.
- Locate the myTable record by name to create a new device timer.
- Get the record number.
This record number will be used as an input parameter for the timer.
- Lock the myTable record for writing.
- Read the timerslot, and check if any device timers are running.
- Delete the old device timer if one is already running.
- Create a new device timer in the Secondtimer table.
- Link the device timer to the myTable record by writing the new Secondtimer slot.
- Unlock the myTable record.
- Unlock the myTable table.
Example code
try
{
IRealTimeDatabase database = new RealTimeDatabase();
// Open the RTDB.
if (!database.IsOpen())
{
database.Open();
}
// Access the myTable table.
using (IRealTimeTable table = new RealTimeTable("myTable"))
{
ITableLockRequestType tableLockRequest = new TableLockRequestType();
IRecordLockRequestType recordLockRequest = new RecordLockRequestType();
// Lock the myTable table for reading.
table.Lock(tableLockRequest.SAFE_READ);
// Locate the myTable record by name to create a new device timer.
var nameField = new RealTimeStringField(table, "name");
using (var record = table.FindRecord(nameField, "myPoint_1"))
{
// Get the record number.
var param0 = record.RecordNumber;
// Lock the myTable record for writing.
record.Lock(recordLockRequest.SAFE_WRITE);
// Read the timerslot, and check if any device timers are running.
if (record.ReadField<Int32>("timerslot") > 0)
{
// Delete the old device timer.
secondTrvdel(record.ReadField<Int32>("timerslot"));
}
// Create a new device timer in the secondtimer table.
var newTimerSlot = secondTricre(record.ReadField<ushort>("timeout"), "mycallback", param0, record.ReadField<Int32>("dataset"),0);
// Link the device timer to the myTable record by writing the new secondtimer slot.
record.WriteField<Int32>("timerslot", newTimerSlot);
} // Leaving scope releases the myTable record lock.
} // Leaving scope releases the myTable table lock.
}
catch (Exception ex)
{
Console.WriteLine(quot;Error creating a device timer in the secondtimer table: {ex.Message}");
result = false;
}