More thread fine-tuning.

This commit is contained in:
Den Delimarsky 2021-04-24 10:07:42 -07:00
parent f277832188
commit 798667d0e5
No known key found for this signature in database
GPG key ID: E1BE1355085F0BCF
2 changed files with 12 additions and 39 deletions

View file

@ -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<bool> callback, bool keepDisplayOn = true)
public static void SetTimedKeepAwake(long seconds, Action<bool> 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); ;
}

View file

@ -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}");