From 395309c67c25e469855389e3cd4507e92b7a680f Mon Sep 17 00:00:00 2001 From: Den Delimarsky <1389609+dend@users.noreply.github.com> Date: Sat, 24 Apr 2021 11:06:13 -0700 Subject: [PATCH] Update with proper event caching --- .../espresso/Espresso.Shell/Core/APIHelper.cs | 68 +++++++++++-------- .../Espresso.Shell/Espresso.Shell.csproj | 1 + .../espresso/Espresso.Shell/Program.cs | 28 +++++++- 3 files changed, 64 insertions(+), 33 deletions(-) diff --git a/src/modules/espresso/Espresso.Shell/Core/APIHelper.cs b/src/modules/espresso/Espresso.Shell/Core/APIHelper.cs index 43682c2f1c..12454cafbf 100644 --- a/src/modules/espresso/Espresso.Shell/Core/APIHelper.cs +++ b/src/modules/espresso/Espresso.Shell/Core/APIHelper.cs @@ -91,52 +91,60 @@ namespace Espresso.Shell.Core private static bool RunTimedLoop(long seconds, bool keepDisplayOn = true) { - bool success; + bool success = false; // In case cancellation was already requested. //ThreadToken.ThrowIfCancellationRequested(); - if (keepDisplayOn) + try { - success = SetAwakeState(EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS); - if (success) + if (keepDisplayOn) { - Console.WriteLine("Timed keep-awake with display on."); - var startTime = DateTime.UtcNow; - while (DateTime.UtcNow - startTime < TimeSpan.FromSeconds(seconds)) + success = SetAwakeState(EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS); + if (success) { - if (ThreadToken.IsCancellationRequested) + Console.WriteLine("Timed keep-awake with display on."); + var startTime = DateTime.UtcNow; + while (DateTime.UtcNow - startTime < TimeSpan.FromSeconds(seconds)) { - ThreadToken.ThrowIfCancellationRequested(); + if (ThreadToken.IsCancellationRequested) + { + ThreadToken.ThrowIfCancellationRequested(); + } } + return success; + } + else + { + return success; } - return success; } else { - return success; + success = SetAwakeState(EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS); + if (success) + { + Console.WriteLine("Timed keep-awake with display off."); + var startTime = DateTime.UtcNow; + while (DateTime.UtcNow - startTime < TimeSpan.FromSeconds(seconds)) + { + if (ThreadToken.IsCancellationRequested) + { + ThreadToken.ThrowIfCancellationRequested(); + } + } + return success; + } + else + { + return success; + } } } - else + catch (OperationCanceledException ex) { - success = SetAwakeState(EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS); - if (success) - { - Console.WriteLine("Timed keep-awake with display off."); - var startTime = DateTime.UtcNow; - while (DateTime.UtcNow - startTime < TimeSpan.FromSeconds(seconds)) - { - if (ThreadToken.IsCancellationRequested) - { - ThreadToken.ThrowIfCancellationRequested(); - } - } - return success; - } - else - { - return success; - } + // Task was clearly cancelled. + return success; } } } diff --git a/src/modules/espresso/Espresso.Shell/Espresso.Shell.csproj b/src/modules/espresso/Espresso.Shell/Espresso.Shell.csproj index fcef0fe5b7..6c3fac8cd6 100644 --- a/src/modules/espresso/Espresso.Shell/Espresso.Shell.csproj +++ b/src/modules/espresso/Espresso.Shell/Espresso.Shell.csproj @@ -8,6 +8,7 @@ + diff --git a/src/modules/espresso/Espresso.Shell/Program.cs b/src/modules/espresso/Espresso.Shell/Program.cs index 9f4f7624c1..abb0f1cf42 100644 --- a/src/modules/espresso/Espresso.Shell/Program.cs +++ b/src/modules/espresso/Espresso.Shell/Program.cs @@ -9,6 +9,7 @@ using System; using System.CommandLine; using System.CommandLine.Invocation; using System.IO; +using System.Runtime.Caching; using System.Threading; namespace Espresso.Shell @@ -20,6 +21,10 @@ namespace Espresso.Shell private static FileSystemWatcher watcher = null; public static Mutex Mutex { get => mutex; set => mutex = value; } + private static MemoryCache _memoryCache; + private static CacheItemPolicy _cacheItemPolicy; + private const int CacheExpirationTimeframe = 500; + static int Main(string[] args) { bool instantiated; @@ -104,13 +109,21 @@ namespace Espresso.Shell try { + _memoryCache = MemoryCache.Default; + watcher = new FileSystemWatcher { Path = Path.GetDirectoryName(config), EnableRaisingEvents = true, - NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size, + NotifyFilter = NotifyFilters.LastWrite, Filter = Path.GetFileName(config) }; + + _cacheItemPolicy = new CacheItemPolicy() + { + RemovedCallback = HandleCacheRemoval + }; + watcher.Changed += new FileSystemEventHandler(HandleEspressoConfigChange); // Initially the file might not be updated, so we need to start processing @@ -147,12 +160,21 @@ namespace Espresso.Shell new ManualResetEvent(false).WaitOne(); } - private static void HandleEspressoConfigChange(object sender, FileSystemEventArgs e) + private static void HandleCacheRemoval(CacheEntryRemovedArguments args) { + if (args.RemovedReason != CacheEntryRemovedReason.Expired) return; + + var fileEvent = (FileSystemEventArgs)args.CacheItem.Value; Console.WriteLine("Resetting keep-awake to normal state due to settings change."); ResetNormalPowerState(); Console.WriteLine("Detected a file change. Reacting..."); - ProcessSettings(e.FullPath); + ProcessSettings(fileEvent.FullPath); + } + + private static void HandleEspressoConfigChange(object sender, FileSystemEventArgs e) + { + _cacheItemPolicy.AbsoluteExpiration = DateTimeOffset.Now.AddMilliseconds(CacheExpirationTimeframe); + _memoryCache.AddOrGetExisting(e.Name, e, _cacheItemPolicy); } private static void ProcessSettings(string fullPath)