diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/CounterGroup.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/CounterGroup.cs index d60be1b83bdd8..aec84922437a3 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/CounterGroup.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/CounterGroup.cs @@ -127,7 +127,7 @@ private void EnableTimer(float pollingIntervalInSeconds) Debug.Assert(Monitor.IsEntered(s_counterGroupLock)); if (pollingIntervalInSeconds <= 0) { - _pollingIntervalInMilliseconds = 0; + DisableTimer(); } else if (_pollingIntervalInMilliseconds == 0 || pollingIntervalInSeconds * 1000 < _pollingIntervalInMilliseconds) { @@ -187,6 +187,7 @@ private void EnableTimer(float pollingIntervalInSeconds) private void DisableTimer() { + Debug.Assert(Monitor.IsEntered(s_counterGroupLock)); _pollingIntervalInMilliseconds = 0; s_counterGroupEnabledList?.Remove(this); } @@ -252,7 +253,8 @@ private void OnTimer() { _timeStampSinceCollectionStarted = now; TimeSpan delta = now - _nextPollingTimeStamp; - if (delta > TimeSpan.Zero && _pollingIntervalInMilliseconds > 0) + delta = _pollingIntervalInMilliseconds > delta.TotalMilliseconds ? TimeSpan.FromMilliseconds(_pollingIntervalInMilliseconds) : delta; + if (_pollingIntervalInMilliseconds > 0) _nextPollingTimeStamp += TimeSpan.FromMilliseconds(_pollingIntervalInMilliseconds * Math.Ceiling(delta.TotalMilliseconds / _pollingIntervalInMilliseconds)); } }