DCode

Covers Engine 2.0.0

Loops

Run lightweight repeated work on activation and inspect its state and timing.

A loop is lightweight work that repeats at a requested frequency and starts when its file is activated. Use it for simple background automation. Use a task when an operator needs explicit start, hold, and stop controls.

Loop Configuration

Set the requested frequency after the loop name. This header requests 10 Hz with timing warnings disabled (body omitted):

loop docs_demo_poll 10 disable_warnings:
Header optionMeaning
Frequency, such as 10Required positive Hz. 10 requests ten iterations per second, with a nominal 100 ms interval.
disable_warningsOptional final flag suppressing timing warnings. Measurements, missed-cycle counts, and callback errors remain available. Omit it to keep timing warnings enabled.

Loop Lifecycle

Activating the file starts the loop. Closing the editor does not stop it. Reactivation replaces the loop; removing its declaration and reactivating removes it. See Runtime Blocks and Activation for replacement, failures, and backend limits.

Removal releases the loop’s authority and retains its channels and values; see shared artifact cleanup.

Loop Execution

Prefer command for commanded writes and set for published measurements or status. These operations show intent and can acquire an available channel claim; ordinary assignments use existing authority. See the authority examples.

Loop Example

Create docs_demo_count_value at 0 in a test project, then activate this file:

loop docs_demo_copy 2:
    set |docs_demo_copy_value| = |docs_demo_count_value|

The loop requests two iterations per second. Change the input and inspect docs_demo_copy_value; it follows on a later iteration. This is scheduled work. A channel calculation instead runs in response to accepted input writes.

Working Variables

A loop has no start: section. Initialize working variables on its first iteration with once::

loop docs_demo_loop_count 2:
    once:
        count = 0
    count = count + 1
    set |docs_demo_loop_count_value| = count

Variables remain available between iterations until reactivation. Running the file again rebuilds the loop and resets its working state. File globals have a different owner; see Values and Scope.

Loop Timing and Diagnostics

Frequency is a scheduling request. Inspect the generated channels to compare it with actual execution:

ChannelMeaning
<name>_target_frequencyRequested iterations per second.
<name>_actual_frequencyObserved iterations per second, including failed iterations.
<name>_execution_timeSmoothed iteration duration in milliseconds, including waits.
<name>_missed_cyclesPlanned iterations skipped because execution fell behind.
<name>_failed_cyclesCaught execution callback failures.

Slow functions and wait / wait_until delay the loop’s calling execution path and can cause missed cycles. They do not suspend just one iteration while later iterations continue. Keep repeated work short; Common Syntax and Functions explains wait behavior, and Execution and Timing explains scheduling costs.

Reference