Update with proper event caching

This commit is contained in:
Den Delimarsky
2021-04-24 11:06:13 -07:00
parent 798667d0e5
commit 395309c67c
3 changed files with 64 additions and 33 deletions

View File

@@ -91,52 +91,60 @@ namespace Espresso.Shell.Core
private static bool RunTimedLoop(long seconds, bool keepDisplayOn = true) private static bool RunTimedLoop(long seconds, bool keepDisplayOn = true)
{ {
bool success; bool success = false;
// In case cancellation was already requested. // In case cancellation was already requested.
//ThreadToken.ThrowIfCancellationRequested(); //ThreadToken.ThrowIfCancellationRequested();
if (keepDisplayOn) try
{ {
success = SetAwakeState(EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS); if (keepDisplayOn)
if (success)
{ {
Console.WriteLine("Timed keep-awake with display on."); success = SetAwakeState(EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS);
var startTime = DateTime.UtcNow; if (success)
while (DateTime.UtcNow - startTime < TimeSpan.FromSeconds(seconds))
{ {
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 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); // Task was clearly cancelled.
if (success) return 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;
}
} }
} }
} }

View File

@@ -8,6 +8,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20071.2" /> <PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20071.2" />
<PackageReference Include="System.Runtime.Caching" Version="6.0.0-preview.1.21102.12" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -9,6 +9,7 @@ using System;
using System.CommandLine; using System.CommandLine;
using System.CommandLine.Invocation; using System.CommandLine.Invocation;
using System.IO; using System.IO;
using System.Runtime.Caching;
using System.Threading; using System.Threading;
namespace Espresso.Shell namespace Espresso.Shell
@@ -20,6 +21,10 @@ namespace Espresso.Shell
private static FileSystemWatcher watcher = null; private static FileSystemWatcher watcher = null;
public static Mutex Mutex { get => mutex; set => mutex = value; } 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) static int Main(string[] args)
{ {
bool instantiated; bool instantiated;
@@ -104,13 +109,21 @@ namespace Espresso.Shell
try try
{ {
_memoryCache = MemoryCache.Default;
watcher = new FileSystemWatcher watcher = new FileSystemWatcher
{ {
Path = Path.GetDirectoryName(config), Path = Path.GetDirectoryName(config),
EnableRaisingEvents = true, EnableRaisingEvents = true,
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.Size, NotifyFilter = NotifyFilters.LastWrite,
Filter = Path.GetFileName(config) Filter = Path.GetFileName(config)
}; };
_cacheItemPolicy = new CacheItemPolicy()
{
RemovedCallback = HandleCacheRemoval
};
watcher.Changed += new FileSystemEventHandler(HandleEspressoConfigChange); watcher.Changed += new FileSystemEventHandler(HandleEspressoConfigChange);
// Initially the file might not be updated, so we need to start processing // Initially the file might not be updated, so we need to start processing
@@ -147,12 +160,21 @@ namespace Espresso.Shell
new ManualResetEvent(false).WaitOne(); 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."); Console.WriteLine("Resetting keep-awake to normal state due to settings change.");
ResetNormalPowerState(); ResetNormalPowerState();
Console.WriteLine("Detected a file change. Reacting..."); 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) private static void ProcessSettings(string fullPath)