diff --git a/RGB.NET.Core/RGBSurface.cs b/RGB.NET.Core/RGBSurface.cs index 18fd275..e7d8bed 100644 --- a/RGB.NET.Core/RGBSurface.cs +++ b/RGB.NET.Core/RGBSurface.cs @@ -23,7 +23,7 @@ namespace RGB.NET.Core /// public static RGBSurface Instance { get; } = new RGBSurface(); - private long _lastUpdateTicks; + private Stopwatch _deltaTimeCounter; private IList _deviceProvider = new List(); private IList _devices = new List(); @@ -60,7 +60,8 @@ namespace RGB.NET.Core /// private RGBSurface() { - _lastUpdateTicks = Stopwatch.GetTimestamp(); + _deltaTimeCounter = Stopwatch.StartNew(); + _sleepCounter = new Stopwatch(); CheckUpdateLoop(); } diff --git a/RGB.NET.Core/RGBSurfaceDeviceEvents.cs b/RGB.NET.Core/RGBSurfaceDeviceEvents.cs index 36acc1b..5d094eb 100644 --- a/RGB.NET.Core/RGBSurfaceDeviceEvents.cs +++ b/RGB.NET.Core/RGBSurfaceDeviceEvents.cs @@ -1,5 +1,4 @@ using System; -using System.Diagnostics; namespace RGB.NET.Core { @@ -83,9 +82,9 @@ namespace RGB.NET.Core { try { - long lastUpdateTicks = _lastUpdateTicks; - _lastUpdateTicks = Stopwatch.GetTimestamp(); - Updating?.Invoke(new UpdatingEventArgs((_lastUpdateTicks - lastUpdateTicks) / 10000000.0)); + double deltaTime = _deltaTimeCounter.Elapsed.TotalSeconds; + _deltaTimeCounter.Restart(); + Updating?.Invoke(new UpdatingEventArgs(deltaTime)); } catch { /* Well ... that's not my fault */ } } diff --git a/RGB.NET.Core/RGBSurfaceUpdater.cs b/RGB.NET.Core/RGBSurfaceUpdater.cs index 86ac31a..01a7c80 100644 --- a/RGB.NET.Core/RGBSurfaceUpdater.cs +++ b/RGB.NET.Core/RGBSurfaceUpdater.cs @@ -12,6 +12,7 @@ namespace RGB.NET.Core private CancellationTokenSource _updateTokenSource; private CancellationToken _updateToken; private Task _updateTask; + private Stopwatch _sleepCounter; // ReSharper disable MemberCanBePrivate.Global @@ -87,12 +88,13 @@ namespace RGB.NET.Core { while (!_updateToken.IsCancellationRequested) { - long preUpdateTicks = Stopwatch.GetTimestamp(); + _sleepCounter.Restart(); Update(); - LastUpdateTime = ((Stopwatch.GetTimestamp() - preUpdateTicks) / 10000.0); - int sleep = (int)((UpdateFrequency * 1000.0) - LastUpdateTime); + _sleepCounter.Stop(); + LastUpdateTime = _sleepCounter.Elapsed.TotalSeconds; + int sleep = (int)((UpdateFrequency * 1000.0) - _sleepCounter.ElapsedMilliseconds); if (sleep > 0) Thread.Sleep(sleep); }