Class ClockBlock

Source block that generates a periodic tick at a configurable rate using a dedicated high-priority background thread.

JSON configuration

Use these block entries inside a pipeline configuration. Input names must match other blocks in that pipeline; device addresses and file paths must match your setup.

JSON configuration:

{
  "Timer": {
    "Type": "Clock",
    "DesiredRate": 200,
    "Path": "C:/Data/session1"
  }
}

Params: None. Rate is configured via DesiredRate.

How Type, Inputs and Params work · Block catalogue and parameter reference

Inheritance
ClockBlock
Inherited Members
Namespace: MOSAIC.Models.FlowControl
Assembly: MOSAIC.dll
Syntax
public class ClockBlock : BaseBlock, INotifyPropertyChanged, INotifyPropertyChanging, ISubscriber, IPublisher, IBaseBlockProps, IJsonExportable, IDisposable, IAsyncDisposable
Remarks

Overview: The clock uses Stopwatch ticks to schedule publications at approximately DesiredRate Hz. Each tick publishes the current Now() timestamp (a monotonic double in seconds since epoch) to all downstream subscribers.

Timing strategy: The loop computes a fixed tick interval (Stopwatch.Frequency / DesiredRate) and uses a catch-up loop to maintain cadence. When ahead of schedule, SpinWait(int) is used instead of Thread.Sleep to avoid OS scheduler granularity (typically 15 ms on Windows), achieving sub-millisecond jitter at rates up to ~2 kHz.

Source block: This block does not consume upstream data; OnReceive(object, object) throws NotImplementedException if called.

CSV logging: When a Path is specified, Dumper automatically logs each published timestamp value.

Constructors

ClockBlock(string, double)

Initializes a new instance of the ClockBlock class.

Declaration
public ClockBlock(string name = "Clock", double desiredRate = 200)
Parameters
Type Name Description
string name

Display name of the block.

double desiredRate

Desired tick rate in Hz.

Fields

_clockDisposed

Declaration
private bool _clockDisposed
Field Value
Type Description
bool

_clockLock

Declaration
private readonly object _clockLock
Field Value
Type Description
object

_running

Indicates whether the clock loop is running. Marked volatile to ensure visibility across threads.

Declaration
private volatile bool _running
Field Value
Type Description
bool

_thread

The worker thread responsible for generating ticks.

Declaration
private Thread? _thread
Field Value
Type Description
Thread

Properties

DumpFilePrefix

Recordings from this block are named <block>_ticks.csv.

Declaration
protected override string DumpFilePrefix { get; }
Property Value
Type Description
string
Overrides
BaseBlock.DumpFilePrefix

JsonTypeName

Block type identifier for JSON serialization. Override for a custom name.

Declaration
protected override string JsonTypeName { get; }
Property Value
Type Description
string
Overrides
BaseBlock.JsonTypeName

MaxInputs

Gets the maximum number of input connections allowed for this block.

Declaration
public override int MaxInputs { get; }
Property Value
Type Description
int
Overrides
BaseBlock.MaxInputs
Remarks

Override in derived classes to specify input requirements. Default is 1 (most blocks accept exactly one input). Use MaxValue for blocks that accept unlimited inputs (e.g., Joiner).

MinInputs

Gets the minimum number of input connections required for this block.

Declaration
public override int MinInputs { get; }
Property Value
Type Description
int
Overrides
BaseBlock.MinInputs
Remarks

Override in derived classes to specify input requirements. Default is 1 (most blocks require exactly one input). Source blocks (devices, generators) should return 0.

Running

Gets a value indicating whether the clock is currently running.

Declaration
public bool Running { get; }
Property Value
Type Description
bool

Methods

ConfigureInput(IServiceProvider, JsonModel)

Creates a ClockBlock from a JSON pipeline definition.

Declaration
public static ClockBlock ConfigureInput(IServiceProvider sp, JsonModel m)
Parameters
Type Name Description
IServiceProvider sp

Service provider for dependency injection.

JsonModel m

JSON model containing name, desired rate, and optional output path.

Returns
Type Description
ClockBlock

A configured ClockBlock instance.

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

Declaration
public override void Dispose()
Overrides
BaseBlock.Dispose()

OnDesiredRateChanged(double, double)

The clock is the rate source for a synthetic pipeline, so a runtime rate change must reach already-running consumers. Force the new rate down the whole chain — overriding rates that downstream blocks latched on a previous run — instead of the default soft propagation, which a latched downstream rate would otherwise block.

Declaration
protected override void OnDesiredRateChanged(double oldRate, double newRate)
Parameters
Type Name Description
double oldRate
double newRate
Overrides
BaseBlock.OnDesiredRateChanged(double, double)

OnReceive(object, object)

Not implemented — this is a source block that does not consume upstream data.

Declaration
protected override void OnReceive(object sender, object value)
Parameters
Type Name Description
object sender
object value
Overrides
BaseBlock.OnReceive(object, object)
Exceptions
Type Condition
NotImplementedException

Always thrown.

Start()

Starts the clock thread if it is not already running.

Declaration
public void Start()
Remarks

The thread runs with Highest and uses ElapsedTicks for scheduling to minimise drift. A catch-up loop ensures that if the thread falls behind (e.g. due to GC), missed ticks are published immediately rather than dropped.

Between ticks, the loop uses SpinWait(int) (when ahead by a small margin) or Yield() (when exactly on schedule) to avoid the ~15 ms granularity of Thread.Sleep.

Stop()

Stops the clock thread and waits for it to terminate.

Declaration
public void Stop()

Implements

INotifyPropertyChanged
INotifyPropertyChanging
ISubscriber
IPublisher
IBaseBlockProps
IJsonExportable
IDisposable
IAsyncDisposable

Search documentation

Start typing to search the documentation.