feat(windows): add sleep blocker for long jobs
SleepBlocker requests ES_CONTINUOUS | ES_SYSTEM_REQUIRED (optionally the display) from a dedicated thread, because the execution state belongs to the calling thread; disposing from any thread clears it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
2 files changed
+82
No files matched your search
@@ -0,0 +1,55 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Titano.Core.Windows.Power;
|
||||
|
||||
/// <summary>
|
||||
/// Keeps the system (and optionally the display) awake during a long job: SetThreadExecutionState with
|
||||
/// ES_CONTINUOUS | ES_SYSTEM_REQUIRED, cleared again on <see cref="Dispose"/>. The execution state belongs to the
|
||||
/// calling thread, so each blocker owns a small dedicated thread; this keeps the request valid even when an async
|
||||
/// job is disposed on a different pool thread than the one that started it.
|
||||
/// </summary>
|
||||
public sealed partial class SleepBlocker : IDisposable
|
||||
{
|
||||
private const uint ES_SYSTEM_REQUIRED = 0x1;
|
||||
private const uint ES_DISPLAY_REQUIRED = 0x2;
|
||||
private const uint ES_CONTINUOUS = 0x80000000;
|
||||
|
||||
private readonly ManualResetEventSlim _release = new();
|
||||
private readonly Thread _owner;
|
||||
private int _disposed;
|
||||
|
||||
/// <param name="keepDisplayOn">Also keep the display on (e.g. while a preview plays).</param>
|
||||
public SleepBlocker(bool keepDisplayOn = false)
|
||||
{
|
||||
var flags = ES_CONTINUOUS | ES_SYSTEM_REQUIRED | (keepDisplayOn ? ES_DISPLAY_REQUIRED : 0);
|
||||
using var applied = new ManualResetEventSlim();
|
||||
_owner = new Thread(() =>
|
||||
{
|
||||
IsActive = SetThreadExecutionState(flags) != 0;
|
||||
applied.Set();
|
||||
_release.Wait();
|
||||
SetThreadExecutionState(ES_CONTINUOUS);
|
||||
})
|
||||
{
|
||||
IsBackground = true,
|
||||
Name = "Titano sleep blocker",
|
||||
};
|
||||
_owner.Start();
|
||||
applied.Wait();
|
||||
}
|
||||
|
||||
/// <summary>False when Windows rejected the request.</summary>
|
||||
public bool IsActive { get; private set; }
|
||||
|
||||
/// <summary>Restores normal sleep behaviour.</summary>
|
||||
public void Dispose()
|
||||
{
|
||||
if (Interlocked.Exchange(ref _disposed, 1) != 0) return;
|
||||
_release.Set();
|
||||
_owner.Join();
|
||||
_release.Dispose();
|
||||
}
|
||||
|
||||
[LibraryImport("kernel32.dll")]
|
||||
private static partial uint SetThreadExecutionState(uint flags);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using Titano.Core.Windows.Power;
|
||||
using Titano.Testing;
|
||||
|
||||
namespace Titano.Core.Windows.Tests.Power;
|
||||
|
||||
[WindowsOnly]
|
||||
public sealed class SleepBlockerTests
|
||||
{
|
||||
[Test]
|
||||
[Timeout(10_000)]
|
||||
public async Task BlocksUntilDisposedFromAnyThread()
|
||||
{
|
||||
var blocker = new SleepBlocker();
|
||||
Assert.True(blocker.IsActive);
|
||||
// Disposing on another pool thread must still clear the request made by the owner thread.
|
||||
await Task.Run(blocker.Dispose);
|
||||
blocker.Dispose();
|
||||
}
|
||||
|
||||
[Test]
|
||||
[Timeout(10_000)]
|
||||
public void DisplayVariantWorks()
|
||||
{
|
||||
using var blocker = new SleepBlocker(keepDisplayOn: true);
|
||||
Assert.True(blocker.IsActive);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user