More thread fine-tuning.

This commit is contained in:
Den Delimarsky
2021-04-24 10:07:42 -07:00
parent f277832188
commit 798667d0e5
2 changed files with 12 additions and 39 deletions

View File

@@ -63,7 +63,7 @@ namespace Espresso.Shell.Core
public static bool SetNormalKeepAwake() public static bool SetNormalKeepAwake()
{ {
//TokenSource.Cancel(); TokenSource.Cancel();
return SetAwakeState(EXECUTION_STATE.ES_CONTINUOUS); return SetAwakeState(EXECUTION_STATE.ES_CONTINUOUS);
} }
@@ -79,22 +79,13 @@ namespace Espresso.Shell.Core
} }
} }
public static void SetTimedKeepAwake(long seconds, Action<bool> callback, bool keepDisplayOn = true) public static void SetTimedKeepAwake(long seconds, Action<bool> callback, Action failureCallback, bool keepDisplayOn = true)
{ {
ThreadToken = TokenSource.Token; ThreadToken = TokenSource.Token;
try Task.Run(() => RunTimedLoop(seconds, keepDisplayOn), ThreadToken)
{ .ContinueWith((result) => callback(result.Result), TaskContinuationOptions.OnlyOnRanToCompletion)
Task.Run(() => RunTimedLoop(seconds, keepDisplayOn), ThreadToken).ContinueWith((result) => callback(result.Result)); .ContinueWith((result) => failureCallback, TaskContinuationOptions.NotOnRanToCompletion); ;
}
catch (OperationCanceledException e)
{
Console.WriteLine($"{nameof(OperationCanceledException)} thrown with message: {e.Message}");
}
finally
{
TokenSource.Dispose();
}
} }

View File

@@ -120,7 +120,6 @@ namespace Espresso.Shell
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"There was a problem with the configuration file. Make sure it exists.\n{ex.Message}"); 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 else
@@ -141,20 +140,7 @@ namespace Espresso.Shell
else else
{ {
// Timed keep-awake. // Timed keep-awake.
APIHelper.SetTimedKeepAwake(timeLimit, LogTimedKeepAwakeCompletion, displayOn); APIHelper.SetTimedKeepAwake(timeLimit, LogTimedKeepAwakeCompletion, LogUnexpectedOrCancelledKeepAwakeCompletion, 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.");
//}
} }
} }
@@ -212,17 +198,8 @@ namespace Espresso.Shell
long computedTime = (settings.Properties.Hours.Value * 60 * 60) + (settings.Properties.Minutes.Value * 60); 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."); Console.WriteLine($"In timed keep-awake mode. Expecting to be awake for {computedTime} seconds.");
APIHelper.SetTimedKeepAwake(computedTime, LogTimedKeepAwakeCompletion, settings.Properties.KeepDisplayOn.Value); APIHelper.SetTimedKeepAwake(computedTime, LogTimedKeepAwakeCompletion, LogUnexpectedOrCancelledKeepAwakeCompletion, settings.Properties.KeepDisplayOn.Value);
//if (success)
//{
// Console.WriteLine($"Finished execution of timed keep-awake.");
// ResetNormalPowerState();
//}
//else
//{
// Console.WriteLine("Could not set up the state to be timed keep awake.");
//}
break; break;
} }
default: 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) private static void LogTimedKeepAwakeCompletion(bool result)
{ {
Console.Write($"Completed timed keep-awake successfully: {result}"); Console.Write($"Completed timed keep-awake successfully: {result}");