Table of Contents

Class TimedEvents

Namespace
Terminal.Gui.App
Assembly
Terminal.Gui.dll

Manages scheduled timeouts (timed callbacks) for the application.

Allows scheduling of callbacks to be invoked after a specified delay, with optional repetition. Timeouts are stored in a sorted list by their scheduled execution time (high-resolution ticks).

Typical usage:

  1. Call Add(TimeSpan, Func<bool>) to schedule a callback.
  2. Call RunTimers() periodically (e.g., from the main loop) to execute due callbacks.
  3. Call Remove(object) to cancel a scheduled timeout.
public class TimedEvents : ITimedEvents
Inheritance
TimedEvents
Implements
Inherited Members

Remarks

Add(TimeSpan, Func<bool>), Add(Timeout), Remove(object), StopAll(), GetTimeout(object), CheckTimers(out int), and RunTimers() are safe to call concurrently from any thread. Timeout callbacks, Span access, Added handlers, and time-provider reads occur outside the timeout queue lock, so user code can schedule or cancel timeouts without deadlocking.

Timeouts returns a snapshot so the queue can be inspected safely while another thread schedules or cancels timeouts.

By default, uses GetTimestamp() for high-resolution timing to provide microsecond-level precision and eliminate race conditions from timer resolution issues.

For testing scenarios, an ITimeProvider can be injected via the constructor to enable virtual time control, allowing tests to run instantly without real delays.

Constructors

TimedEvents()

Initializes a new instance of TimedEvents with the default system time provider.

TimedEvents(ITimeProvider?)

Initializes a new instance of TimedEvents with the specified time provider.

Properties

Timeouts

Gets the list of all timeouts sorted by the TimeSpan time ticks. A shorter limit time can be added at the end, but it will be called before an earlier addition that has a longer limit time.

Methods

Add(TimeSpan, Func<bool>)

Adds a timeout to the application.

Add(Timeout)

Adds a timeout to the application.

CheckTimers(out int)

Determines whether any timeout is queued and calculates how long the caller may wait before the earliest one is due.

GetTimeout(object)

Gets the configured interval for a queued timeout occurrence associated with the specified token.

Remove(object)

Cancels all timeout occurrences associated with a previously returned token.

RunTimers()

Runs timeouts that are due.

StopAll()

Stops and removes all timed events.

Events

Added

Invoked when a new timeout is added. To be used in the case when StopAfterFirstIteration is true.