mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 03:37:59 +01:00
Adding the option to have timed keep-awake
This commit is contained in:
@@ -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);
|
||||
}
|
||||
@@ -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)
|
||||
{
|
||||
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
|
||||
{
|
||||
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.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,6 +82,24 @@ namespace Espresso.Shell
|
||||
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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user