Fix timer calculations mixing Stopwatch and TimeSpan ticks#602
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The timer implementation mixes two different time units:
Stopwatch.GetTimestamp(), which returns timestamps expressed in Stopwatch ticks (Stopwatch.Frequencyticks 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
Here
Stopwatch.GetTimestamp()is expressed in Stopwatch ticks, whileinterval.Ticksis expressed in TimeSpan ticks. These are different units and should not be combined directly.This issue is less visible on Windows because
Stopwatch.Frequencycommonly matchesTimeSpan.TicksPerSecond(10,000,000).On Linux, however,
Stopwatch.Frequencyis typically much higher (for example 1,000,000,000), causing timer intervals to be interpreted incorrectly.For example:
As a result, a 1-second interval is interpreted as:
which causes timers to execute approximately 100× faster than expected.
Solution
Convert
TimeSpanvalues to Stopwatch ticks before storing or comparing them:This ensures that all timer calculations use the same unit (
Stopwatchticks) and behave consistently across platforms.