From 3a6e02db1d89f5b6e5040658eda0ac946658893d Mon Sep 17 00:00:00 2001 From: Joel Ahlgren Date: Mon, 9 Jan 2017 23:07:37 +0100 Subject: [PATCH] Fixed bug with hybrid boot (fast start) in Windows 8/10, where computer would shutdown immediately. Also moved time to wait to a Setting. --- IdleStartWatchdog/App.config | 12 ++++++ IdleStartWatchdog/IdleStartWatchdog.csproj | 13 ++++-- IdleStartWatchdog/Service.cs | 46 ++++++++++------------ IdleStartWatchdog/ServiceStates.cs | 5 --- IdleStartWatchdog/Watchdog.Designer.cs | 38 ++++++++++++++++++ IdleStartWatchdog/Watchdog.settings | 9 +++++ 6 files changed, 90 insertions(+), 33 deletions(-) create mode 100644 IdleStartWatchdog/Watchdog.Designer.cs create mode 100644 IdleStartWatchdog/Watchdog.settings diff --git a/IdleStartWatchdog/App.config b/IdleStartWatchdog/App.config index 87b8caf..60575cc 100644 --- a/IdleStartWatchdog/App.config +++ b/IdleStartWatchdog/App.config @@ -1,6 +1,18 @@ + + +
+ + + + + + 00:10:00 + + + diff --git a/IdleStartWatchdog/IdleStartWatchdog.csproj b/IdleStartWatchdog/IdleStartWatchdog.csproj index 240428b..347ebd8 100644 --- a/IdleStartWatchdog/IdleStartWatchdog.csproj +++ b/IdleStartWatchdog/IdleStartWatchdog.csproj @@ -56,9 +56,7 @@ icon.ico - - true - + ..\packages\Cassia.2.0.0.60\lib\2.0\Cassia.dll @@ -77,11 +75,20 @@ + + True + True + Watchdog.settings + + + SettingsSingleFileGenerator + Watchdog.Designer.cs + diff --git a/IdleStartWatchdog/Service.cs b/IdleStartWatchdog/Service.cs index a7e6aa9..c2dc9be 100644 --- a/IdleStartWatchdog/Service.cs +++ b/IdleStartWatchdog/Service.cs @@ -11,7 +11,6 @@ public partial class Service : ServiceBase { private EventLog _log; private readonly Timer _timer; - private const int MinutesToWaitBeforeShutdown = 10; private ServiceStates _currentState; private DateTime _lastTimeUserLoggedOut; private bool _shutdownInitiated; @@ -19,9 +18,6 @@ public partial class Service : ServiceBase public Service() { InitializeComponent(); - _currentState = ServiceStates.InitialStart; - _lastTimeUserLoggedOut = DateTime.MinValue; - _shutdownInitiated = false; _log = new EventLog("Application", ".", "IdleStartWatchdog"); _timer = new Timer(1000) {AutoReset = true}; @@ -30,22 +26,7 @@ public Service() private void CheckStatus(object sender, ElapsedEventArgs elapsedEventArgs) { - if (_currentState == ServiceStates.InitialStart) - { - // If someone has logged in, this service is done. - if (IsSomeoneLoggedOn) - { - Log("User logged on, switching state."); - _currentState = ServiceStates.UserLoggedIn; - } - else if (TimeSpan.FromMilliseconds(Environment.TickCount) > TimeSpan.FromMinutes(MinutesToWaitBeforeShutdown)) - { - // Check if computer has been running for long enough to shut it down. - Log($"No-one logged on after {MinutesToWaitBeforeShutdown} minutes, shutting down computer.", EventLogEntryType.Warning); - ShutdownComputer(); - } - } - else if (_currentState == ServiceStates.UserLoggedIn) + if (_currentState == ServiceStates.UserLoggedIn) { if (!IsSomeoneLoggedOn) { @@ -58,15 +39,15 @@ private void CheckStatus(object sender, ElapsedEventArgs elapsedEventArgs) { if (IsSomeoneLoggedOn) { - Log("Someone logged in again."); + Log("User logged in."); _currentState = ServiceStates.UserLoggedIn; } else { var loggedOut = DateTime.Now - _lastTimeUserLoggedOut; - if (loggedOut > TimeSpan.FromMinutes(MinutesToWaitBeforeShutdown)) + if (loggedOut > Watchdog.Default.TimeToWait) { - Log($"No-one has signed in again after {MinutesToWaitBeforeShutdown} minutes, shutting down computer.", EventLogEntryType.Warning); + Log($"No-one has signed in after {Watchdog.Default.TimeToWait.ToString()}, shutting down computer.", EventLogEntryType.Warning); ShutdownComputer(); } } @@ -75,15 +56,30 @@ private void CheckStatus(object sender, ElapsedEventArgs elapsedEventArgs) protected override void OnStart(string[] args) { + Initialize(); _timer.Start(); } protected override void OnStop() { - if (_timer != null) + _timer.Stop(); + } + + protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus) + { + if (powerStatus == PowerBroadcastStatus.ResumeSuspend) { - _timer.Stop(); + Initialize(); } + + return base.OnPowerEvent(powerStatus); + } + + private void Initialize() + { + _currentState = IsSomeoneLoggedOn ? ServiceStates.UserLoggedIn : ServiceStates.UserLoggedOut; + _lastTimeUserLoggedOut = DateTime.Now; + _shutdownInitiated = false; } private void ShutdownComputer() diff --git a/IdleStartWatchdog/ServiceStates.cs b/IdleStartWatchdog/ServiceStates.cs index e20ad76..ee8cd38 100644 --- a/IdleStartWatchdog/ServiceStates.cs +++ b/IdleStartWatchdog/ServiceStates.cs @@ -5,11 +5,6 @@ /// public enum ServiceStates { - /// - /// Computer has started, but noone has logged in. - /// - InitialStart, - /// /// User is currently logged in. /// diff --git a/IdleStartWatchdog/Watchdog.Designer.cs b/IdleStartWatchdog/Watchdog.Designer.cs new file mode 100644 index 0000000..f533e95 --- /dev/null +++ b/IdleStartWatchdog/Watchdog.Designer.cs @@ -0,0 +1,38 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace IdleStartWatchdog { + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "14.0.0.0")] + internal sealed partial class Watchdog : global::System.Configuration.ApplicationSettingsBase { + + private static Watchdog defaultInstance = ((Watchdog)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Watchdog()))); + + public static Watchdog Default { + get { + return defaultInstance; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("00:10:00")] + public global::System.TimeSpan TimeToWait { + get { + return ((global::System.TimeSpan)(this["TimeToWait"])); + } + set { + this["TimeToWait"] = value; + } + } + } +} diff --git a/IdleStartWatchdog/Watchdog.settings b/IdleStartWatchdog/Watchdog.settings new file mode 100644 index 0000000..3574c23 --- /dev/null +++ b/IdleStartWatchdog/Watchdog.settings @@ -0,0 +1,9 @@ + + + + + + 00:10:00 + + + \ No newline at end of file