GhostTick 2.0.0

dotnet add package GhostTick --version 2.0.0
                    
NuGet\Install-Package GhostTick -Version 2.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GhostTick" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="GhostTick" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="GhostTick" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add GhostTick --version 2.0.0
                    
#r "nuget: GhostTick, 2.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package GhostTick@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=GhostTick&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=GhostTick&version=2.0.0
                    
Install as a Cake Tool

GhostTick

CI NuGet Version NuGet Downloads .NET License: MIT

English | 简体中文

A high-precision timer toolkit for .NET that delivers events through System.Threading.Channels.

Features

  • Sub-millisecond precision — hybrid sleep + busy-spin strategy; Reduce OS timer granularity from ~15 ms to ~1 ms on Windows
  • Channel-based API — consumers receive TimerEvent values via ChannelReader<TimerEvent>, keeping the timer thread fully decoupled from consumer latency
  • Drift correction — GhostTicker computes every target as start + seq × interval, so accumulated error stays bounded over time
  • Target framework — net10.0

Installation

dotnet add package GhostTick

Quick start

Repeating ticker

using var ticker = new GhostTicker(TimeSpan.FromMilliseconds(16.67)); // ~60 fps
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));

await foreach (var evt in ticker.Reader.ReadAllAsync(cts.Token))
{
    // evt.Sequence — monotonic tick counter (gaps indicate dropped ticks)
    // evt.Drift    — how late this tick fired vs its scheduled time
    Render(evt.Sequence);
}

API reference

TimerEvent

Member Type Description
ScheduledAt DateTimeOffset UTC time the event was expected to fire
FiredAt DateTimeOffset UTC time the event actually fired (Stopwatch-derived, sub-ms accurate)
Sequence ulong Increments per tick
Drift TimeSpan FiredAt − ScheduledAt — positive means late

GhostTicker

new GhostTicker(TimeSpan interval, GhostTickerOptions? options = null)
Member Description
Reader ChannelReader<TimerEvent> — completes on stop
Stop() Signal the tick loop to exit and complete the channel
Dispose() Same as Stop()

Options

GhostTickerOptions

Property Default Description
SpinThreshold 1.5 ms Switch from sleep to busy-spin this far before each target
ChannelCapacity 1 Bounded channel capacity
FullMode DropOldest What to do when the channel is full (slow consumer)
ThreadPriority AboveNormal Priority of the dedicated ticker thread
ThreadName auto Name visible in debuggers / profilers

Slow consumers and back-pressure

The timer thread calls TryWrite and never blocks. When the channel is full:

  • DropOldest (default) — oldest pending tick is discarded; consumer always gets the freshest event. Gaps in Sequence reveal how many ticks were dropped.
  • DropNewest — new tick is discarded; consumer drains at its own pace.
  • Wait — has no effect on the timer thread; TryWrite is always used so ticks are still dropped when the channel is full.

Fan-out to multiple consumers

ChannelReader<T> delivers each item to exactly one reader. For broadcast semantics, dispatch manually:

using var ticker = new GhostTicker(TimeSpan.FromMilliseconds(100));

var ch1 = Channel.CreateBounded<TimerEvent>(4);
var ch2 = Channel.CreateBounded<TimerEvent>(4);

// Dispatcher
_ = Task.Run(async () =>
{
    await foreach (var evt in ticker.Reader.ReadAllAsync())
    {
        ch1.Writer.TryWrite(evt);
        ch2.Writer.TryWrite(evt);
    }
    ch1.Writer.Complete();
    ch2.Writer.Complete();
});

// Consumer A
_ = Task.Run(async () =>
{
    await foreach (var evt in ch1.Reader.ReadAllAsync())
        Console.WriteLine($"A #{evt.Sequence}");
});

Benchmarks

Environment: Windows 11, 13th Gen Intel Core i9-13980HX, .NET 10.0.14, BenchmarkDotNet v0.15.8

Fire accuracy — error vs scheduled time (µs, lower is better)

100 samples per cell.

Method Delay Min Mean StdDev P95 P99
GhostTicker 1 ms 0.0 0.3 0.5 0.5 0.7
Task.Delay 1 ms 13,598 14,421 375 15,052 15,129
Threading.Timer 1 ms 13,647 14,384 407 15,072 15,209
Timers.Timer 1 ms 13,636 14,428 377 15,059 15,146
GhostTicker 5 ms 0.1 0.7 1.4 0.9 8.4
Task.Delay 5 ms 9,646 10,369 437 11,190 11,438
Threading.Timer 5 ms 9,717 10,459 453 11,079 11,680
Timers.Timer 5 ms 9,533 10,376 425 11,103 11,148
GhostTicker 10 ms 0.3 2.3 10.2 4.1 42.4
Task.Delay 10 ms 4,542 5,455 430 6,061 6,675
Threading.Timer 10 ms 4,594 5,417 436 6,106 6,388
Timers.Timer 10 ms 4,744 5,409 427 6,039 6,167

At 1 ms delay, GhostTicker mean error is 0.3 µs vs ~14,400 µs for all others — a ~48,000× improvement.

Tick cadence — 100 consecutive ticks, total wall time (ms, lower is better)

GhostTicker uses a dedicated AboveNormal-priority thread; others depend on the thread pool.

Method Interval Mean Ratio Allocated
GhostTicker 1 ms 100.1 ms 1.00 16.64 KB
PeriodicTimer 1 ms 1,542.9 ms 15.42× 2.32 KB
Threading.Timer 1 ms 1,542.0 ms 15.41× 2.80 KB
GhostTicker 10 ms 1,000.1 ms 1.00 16.87 KB
PeriodicTimer 10 ms 1,544.4 ms 1.54× 14.37 KB
Threading.Timer 10 ms 1,539.8 ms 1.54× 2.83 KB
GhostTicker 50 ms 5,000.1 ms 1.00 17.03 KB
PeriodicTimer 50 ms 5,008.2 ms 1.00× 2.32 KB
Threading.Timer 50 ms 5,006.5 ms 1.00× 2.80 KB

Run benchmarks locally:

dotnet run --project benchmarks/GhostTick.Benchmarks -c Release

Examples

See examples/GhostTick.Examples for runnable examples covering all features.

dotnet run --project examples/GhostTick.Examples

License

MIT

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 105 11/10/2026
1.0.4 181 5/26/2026
1.0.2 133 4/20/2026
1.0.1 135 4/16/2026
1.0.0 133 4/16/2026