The most robust method involves using the built-in PlcTaskSystemInfo structure. This provides real-time data about the current task. _TaskInfo[index].FirstCycle
With TwinCAT 3, Beckhoff introduced the Tc3_Standard library, which includes a dedicated function block: F_TRIG combined with a system flag is no longer needed. Instead, use:
// 2. Clear alarms GVL.stAlarmBuffer := (Count := 0);