Skip to content

Fix timer calculations mixing Stopwatch and TimeSpan ticks#602

Merged
ikkentim merged 3 commits into
ikkentim:mainfrom
DevD4v3:patch-7
Jun 13, 2026
Merged

Fix timer calculations mixing Stopwatch and TimeSpan ticks#602
ikkentim merged 3 commits into
ikkentim:mainfrom
DevD4v3:patch-7

Conversation

@DevD4v3

@DevD4v3 DevD4v3 commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Problem

The timer implementation mixes two different time units:

  • Stopwatch.GetTimestamp(), which returns timestamps expressed in Stopwatch ticks (Stopwatch.Frequency ticks per second).
  • TimeSpan.Ticks, which uses a fixed resolution of 10,000,000 ticks per second.

These values are currently used interchangeably when scheduling timers.

Example

nextTick = Stopwatch.GetTimestamp() + interval.Ticks;

Here Stopwatch.GetTimestamp() is expressed in Stopwatch ticks, while interval.Ticks is expressed in TimeSpan ticks. These are different units and should not be combined directly.

This issue is less visible on Windows because Stopwatch.Frequency commonly matches TimeSpan.TicksPerSecond (10,000,000).

On Linux, however, Stopwatch.Frequency is typically much higher (for example 1,000,000,000), causing timer intervals to be interpreted incorrectly.

For example:

TimeSpan:
1 second = 10,000,000 ticks

Stopwatch (Linux):
1 second = 1,000,000,000 ticks

As a result, a 1-second interval is interpreted as:

10,000,000 / 1,000,000,000 = 0.01 seconds

which causes timers to execute approximately 100× faster than expected.

Solution

Convert TimeSpan values to Stopwatch ticks before storing or comparing them:

var intervalTicks =
    (long)(interval.TotalSeconds * Stopwatch.Frequency);

This ensures that all timer calculations use the same unit (Stopwatch ticks) and behave consistently across platforms.

@DevD4v3 DevD4v3 changed the title fix timer inconsistency on Linux due to mixed time units Fix timer calculations mixing Stopwatch and TimeSpan ticks Jun 12, 2026
@ikkentim ikkentim merged commit 43d7279 into ikkentim:main Jun 13, 2026
1 check passed
@DevD4v3 DevD4v3 deleted the patch-7 branch June 13, 2026 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants