Update with proper event caching
This commit is contained in:
parent
798667d0e5
commit
395309c67c
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||
<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>
|
||||
|
||||
</Project>
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue