Skip to content

EntropyEngine::Core::Timer

A scheduled task that executes after a delay, optionally repeating. More…

#include <Timer.h>

Name
using std::function< void()>WorkFunction
using std::chrono::steady_clock::time_pointTimePoint
using std::chrono::steady_clock::durationDuration
Name
~Timer()
Destroys the timer and cancels it if still valid.
Timer &operator=(Timer && other)
Move assignment - transfers ownership.
Timer &operator=(const Timer & ) =delete
boolisValid() const
Checks if the timer is still active.
boolisRepeating() const
Checks if this is a repeating timer.
voidinvalidate()
Cancels the timer and prevents future executions.
DurationgetInterval() const
Gets the interval for repeating timers.
Timer() =default
Creates an invalid timer (no-op).
Timer(Timer && other)
Move constructor - transfers ownership.
Timer(const Timer & ) =delete
Name
classTimerService
class EntropyEngine::Core::Timer;

A scheduled task that executes after a delay, optionally repeating.

Timer provides an NSTimer-style interface for scheduling work with delays. Timers can be one-shot (fire once) or repeating (fire at intervals). All timing is handled automatically by the TimerService using yieldable WorkGraph nodes.

Key features:

  • One-shot and repeating timers
  • Thread-safe cancellation
  • Main thread or background execution
  • RAII-safe: timers invalidate on destruction
  • No manual memory management

Perfect for:

  • Delayed UI updates
  • Periodic polling
  • Timeout handling
  • Animation frames
  • Network retry logic
// One-shot timer (fires once after delay)
auto timer = timerService.scheduleTimer(
std::chrono::milliseconds(500),
[]{ LOG_INFO("Timer fired!"); },
false // One-shot
);
// Repeating timer (fires every interval)
auto repeating = timerService.scheduleTimer(
std::chrono::seconds(1),
[]{ updateStats(); },
true // Repeating
);
// Cancel timer early
repeating.invalidate();
// Main thread timer for UI updates
auto uiTimer = timerService.scheduleTimer(
std::chrono::milliseconds(16), // ~60 FPS
[]{ updateUI(); },
true, // Repeating
ExecutionType::MainThread
);
using EntropyEngine::Core::Timer::WorkFunction = std::function<void()>;
using EntropyEngine::Core::Timer::TimePoint = std::chrono::steady_clock::time_point;
using EntropyEngine::Core::Timer::Duration = std::chrono::steady_clock::duration;
~Timer()

Destroys the timer and cancels it if still valid.

RAII cleanup - ensures timers are cancelled when they go out of scope. Safe to destroy after manual invalidation.

Timer & operator=(
Timer && other
)

Move assignment - transfers ownership.

Invalidates the current timer before taking ownership of the other. The moved-from timer becomes invalid.

Timer & operator=(
const Timer &
) =delete
bool isValid() const

Checks if the timer is still active.

Return: true if the timer is valid and hasn’t been cancelled

if (timer.isValid()) {
LOG_INFO("Timer still pending");
} else {
LOG_INFO("Timer fired or was cancelled");
}
inline bool isRepeating() const

Checks if this is a repeating timer.

Return: true if the timer repeats, false for one-shot

void invalidate()

Cancels the timer and prevents future executions.

Thread-safe. Safe to call multiple times. After invalidation, the timer will not fire again. For one-shot timers, this prevents the single execution. For repeating timers, this stops all future repetitions.

auto timer = service.scheduleTimer(
std::chrono::seconds(5),
[]{ doWork(); }
);
// Changed our mind
timer.invalidate(); // Work will never execute
inline Duration getInterval() const

Gets the interval for repeating timers.

Return: The interval duration, or zero for one-shot timers

Timer() =default

Creates an invalid timer (no-op).

Default-constructed timers do nothing and are already invalidated. Use TimerService::scheduleTimer() to create active timers.

Timer(
Timer && other
)

Move constructor - transfers ownership.

The moved-from timer becomes invalid. Only one Timer can own a scheduled task at a time.

Timer(
const Timer &
) =delete
friend class TimerService(
TimerService
);

Updated on 2026-01-26 at 17:14:35 -0500