From 798667d0e564a843172ea80ff3e3f3bd33eb2a2a Mon Sep 17 00:00:00 2001 From: Den Delimarsky <1389609+dend@users.noreply.github.com> Date: Sat, 24 Apr 2021 10:07:42 -0700 Subject: [PATCH] More thread fine-tuning. --- .../espresso/Espresso.Shell/Core/APIHelper.cs | 19 +++-------- .../espresso/Espresso.Shell/Program.cs | 32 ++++--------------- 2 files changed, 12 insertions(+), 39 deletions(-) diff --git a/src/modules/espresso/Espresso.Shell/Core/APIHelper.cs b/src/modules/espresso/Espresso.Shell/Core/APIHelper.cs index f7383b3b3c..43682c2f1c 100644 --- a/src/modules/espresso/Espresso.Shell/Core/APIHelper.cs +++ b/src/modules/espresso/Espresso.Shell/Core/APIHelper.cs @@ -63,7 +63,7 @@ namespace Espresso.Shell.Core public static bool SetNormalKeepAwake() { - //TokenSource.Cancel(); + TokenSource.Cancel(); return SetAwakeState(EXECUTION_STATE.ES_CONTINUOUS); } @@ -79,22 +79,13 @@ namespace Espresso.Shell.Core } } - public static void SetTimedKeepAwake(long seconds, Action callback, bool keepDisplayOn = true) + public static void SetTimedKeepAwake(long seconds, Action callback, Action failureCallback, bool keepDisplayOn = true) { ThreadToken = TokenSource.Token; - try - { - Task.Run(() => RunTimedLoop(seconds, keepDisplayOn), ThreadToken).ContinueWith((result) => callback(result.Result)); - } - catch (OperationCanceledException e) - { - Console.WriteLine($"{nameof(OperationCanceledException)} thrown with message: {e.Message}"); - } - finally - { - TokenSource.Dispose(); - } + Task.Run(() => RunTimedLoop(seconds, keepDisplayOn), ThreadToken) + .ContinueWith((result) => callback(result.Result), TaskContinuationOptions.OnlyOnRanToCompletion) + .ContinueWith((result) => failureCallback, TaskContinuationOptions.NotOnRanToCompletion); ; } diff --git a/src/modules/espresso/Espresso.Shell/Program.cs b/src/modules/espresso/Espresso.Shell/Program.cs index a44d21c98a..9f4f7624c1 100644 --- a/src/modules/espresso/Espresso.Shell/Program.cs +++ b/src/modules/espresso/Espresso.Shell/Program.cs @@ -120,7 +120,6 @@ namespace Espresso.Shell catch (Exception ex) { Console.WriteLine($"There was a problem with the configuration file. Make sure it exists.\n{ex.Message}"); - //ForceExit($"There was a problem with the configuration file. Make sure it exists.\n{ex.Message}", 1); } } else @@ -141,20 +140,7 @@ namespace Espresso.Shell else { // Timed keep-awake. - APIHelper.SetTimedKeepAwake(timeLimit, LogTimedKeepAwakeCompletion, displayOn); - //if (success) - //{ - // Console.WriteLine($"Finished execution of timed keep-awake."); - - // // Because the timed keep-awake execution completed, there is no reason for - // // Espresso to stay alive - I will just shut down the application until it's - // // launched again by the user. - // Environment.Exit(0); - //} - //else - //{ - // Console.WriteLine("Could not set up the state to be timed keep awake."); - //} + APIHelper.SetTimedKeepAwake(timeLimit, LogTimedKeepAwakeCompletion, LogUnexpectedOrCancelledKeepAwakeCompletion, displayOn); } } @@ -212,17 +198,8 @@ namespace Espresso.Shell long computedTime = (settings.Properties.Hours.Value * 60 * 60) + (settings.Properties.Minutes.Value * 60); Console.WriteLine($"In timed keep-awake mode. Expecting to be awake for {computedTime} seconds."); - APIHelper.SetTimedKeepAwake(computedTime, LogTimedKeepAwakeCompletion, settings.Properties.KeepDisplayOn.Value); - //if (success) - //{ - // Console.WriteLine($"Finished execution of timed keep-awake."); + APIHelper.SetTimedKeepAwake(computedTime, LogTimedKeepAwakeCompletion, LogUnexpectedOrCancelledKeepAwakeCompletion, settings.Properties.KeepDisplayOn.Value); - // ResetNormalPowerState(); - //} - //else - //{ - // Console.WriteLine("Could not set up the state to be timed keep awake."); - //} break; } default: @@ -248,6 +225,11 @@ namespace Espresso.Shell } } + private static void LogUnexpectedOrCancelledKeepAwakeCompletion() + { + Console.Write("The keep-awake thread was terminated early."); + } + private static void LogTimedKeepAwakeCompletion(bool result) { Console.Write($"Completed timed keep-awake successfully: {result}");