Permalink
Browse files

Suppression of negative Elapsed in Stopwatch

There was a bug (https://connect.microsoft.com/VisualStudio/feedbackdetail/view/94083/stopwatch-returns-negative-elapsed-time) in the old Microsoft .NET versions: the Stopwatch.Elapsed property could return negative value for a small time period. It was be fixed in .NET 4.0, but it can be still reproduced by the current Mono version.
For example, this can be reproduced during dynamic processor frequency scaling (see http://en.wikipedia.org/wiki/SpeedStep).
  • Loading branch information...
AndreyAkinshin committed Aug 19, 2014
1 parent 74ad56c commit dbc021772a8c0a8bf97615523c73d55cf9b376c3
Showing with 4 additions and 0 deletions.
  1. +4 −0 mcs/class/System/System.Diagnostics/Stopwatch.cs
@@ -113,6 +113,10 @@ public void Stop ()
if (!is_running)
return;
elapsed += GetTimestamp () - started;
+#if NET_4_0
+ if (elapsed < 0)
+ elapsed = 0;
+#endif
is_running = false;
}

0 comments on commit dbc0217

Please sign in to comment.