From a795e385979f8c6d96c6e5036b54665c6700a98b Mon Sep 17 00:00:00 2001 From: Mladen Mihajlovic Date: Thu, 6 Sep 2018 21:12:38 +0200 Subject: [PATCH] Fix for timer interval - Fixes #29 Also fix for dispose method on UserActivityHook throwing exception --- Mamesaver/Power/PowerManager.cs | 4 ++-- Mamesaver/Windows/UserActivityHook.cs | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/Mamesaver/Power/PowerManager.cs b/Mamesaver/Power/PowerManager.cs index 374e48b..bc62715 100644 --- a/Mamesaver/Power/PowerManager.cs +++ b/Mamesaver/Power/PowerManager.cs @@ -76,7 +76,7 @@ public void Initialise() powerType.Value.ToString(), _sleepTimeout.TotalMinutes); // Create a timer which fires once when the display or computer should go to sleep - _sleepTimer = new Timer { Interval = (int)_sleepTimeout.TotalMilliseconds, AutoReset = false }; + _sleepTimer = new Timer { Interval = Math.Min(_sleepTimeout.TotalMilliseconds, int.MaxValue), AutoReset = false }; _sleepTimer.Elapsed += SleepTimerTick; _sleepTimer.Start(); @@ -125,7 +125,7 @@ private void PowerStateChanged(object sender, EventArgs args) powerType.Value.ToString(), _sleepTimeout.TotalMinutes); // Update sleep timer on power state change due to different sleep configurations - _sleepTimer.Interval = (int)_sleepTimeout.TotalMilliseconds; + _sleepTimer.Interval = Math.Min(_sleepTimeout.TotalMilliseconds, int.MaxValue); _sleepTimer.Stop(); _sleepTimer.Start(); diff --git a/Mamesaver/Windows/UserActivityHook.cs b/Mamesaver/Windows/UserActivityHook.cs index dda76f1..233b6b7 100644 --- a/Mamesaver/Windows/UserActivityHook.cs +++ b/Mamesaver/Windows/UserActivityHook.cs @@ -479,20 +479,21 @@ public UserActivityHook(bool InstallMouseHook, bool InstallKeyboardHook) Start(InstallMouseHook, InstallKeyboardHook); } - public void Dispose() + protected virtual void Dispose(bool disposing) { - //uninstall hooks and do not throw exceptions - Stop(true, true, false); + if (disposing) + { + Stop(true, true, false); + } } - - /// - /// Destruction. - /// - ~UserActivityHook() + public void Dispose() { - Dispose(); + Dispose(true); + GC.SuppressFinalize(this); } + ~UserActivityHook() => Dispose(false); + /// /// Occurs when the user moves the mouse, presses any mouse button or scrolls the wheel ///