Stopwatch has the following code to detect negative elapsed durations, but this logic only runs during the Stopwatch.Stop call.
|
if (_elapsed < 0) |
|
{ |
|
// When measuring small time periods the Stopwatch.Elapsed* |
|
// properties can return negative values. This is due to |
|
// bugs in the basic input/output system (BIOS) or the hardware |
|
// abstraction layer (HAL) on machines with variable-speed CPUs |
|
// (e.g. Intel SpeedStep). |
|
|
|
_elapsed = 0; |
|
} |
If this behavior is worth preserving for stopped Stopwatch instances, I don't see why the responsibility should be shifted to the caller for "active" ones.
I believe this logic should either be removed entirely, or apply to all overloads regardless of IsRunning, possibly including the new GetElapsedTime helpers.
I wouldn't be surprised if some of our own logic is not accounting for the possibility of negative durations.
Stopwatchhas the following code to detect negative elapsed durations, but this logic only runs during theStopwatch.Stopcall.runtime/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Stopwatch.cs
Lines 63 to 72 in a1f1ab8
If this behavior is worth preserving for stopped
Stopwatchinstances, I don't see why the responsibility should be shifted to the caller for "active" ones.I believe this logic should either be removed entirely, or apply to all overloads regardless of
IsRunning, possibly including the newGetElapsedTimehelpers.I wouldn't be surprised if some of our own logic is not accounting for the possibility of negative durations.