Adding the option to have timed keep-awake

This commit is contained in:
Den Delimarsky
2021-04-08 16:30:56 -07:00
parent 18f94fc0e6
commit cd647eb352
2 changed files with 50 additions and 5 deletions

View File

@@ -49,9 +49,9 @@ namespace Espresso.Shell.Core
} }
} }
public static bool SetIndefiniteKeepAwake(bool? keepDisplayOn = true) public static bool SetIndefiniteKeepAwake(bool keepDisplayOn = true)
{ {
if ((bool)keepDisplayOn) if (keepDisplayOn)
{ {
return SetAwakeState(EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS); return SetAwakeState(EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS);
} }
@@ -61,15 +61,42 @@ namespace Espresso.Shell.Core
} }
} }
public static bool SetTimedKeepAwake(int seconds, bool keepDisplayOn = true) public static bool SetTimedKeepAwake(long seconds, bool keepDisplayOn = true)
{ {
if (keepDisplayOn) if (keepDisplayOn)
{ {
return false; var success = SetAwakeState(EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS);
if (success)
{
RunTimedLoop(seconds);
return true;
}
else
{
return false;
}
} }
else else
{ {
return false; var success = SetAwakeState(EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS);
if (success)
{
RunTimedLoop(seconds);
return true;
}
else
{
return false;
}
}
}
private static void RunTimedLoop(long seconds)
{
var startTime = DateTime.UtcNow;
while (DateTime.UtcNow - startTime < TimeSpan.FromSeconds(seconds))
{
// We do nothing.
} }
} }
} }

View File

@@ -82,6 +82,24 @@ namespace Espresso.Shell
Console.WriteLine("Could not set up the state to be indefinite keep awake."); Console.WriteLine("Could not set up the state to be indefinite keep awake.");
} }
} }
else
{
// Timed keep-awake.
bool success = APIHelper.SetTimedKeepAwake(timeLimit, 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.");
}
}
new ManualResetEvent(false).WaitOne(); new ManualResetEvent(false).WaitOne();
} }