Dapplo.Windows.SystemState
3.14.4
Prefix Reserved
dotnet add package Dapplo.Windows.SystemState --version 3.14.4
NuGet\Install-Package Dapplo.Windows.SystemState -Version 3.14.4
<PackageReference Include="Dapplo.Windows.SystemState" Version="3.14.4" />
<PackageVersion Include="Dapplo.Windows.SystemState" Version="3.14.4" />
<PackageReference Include="Dapplo.Windows.SystemState" />
paket add Dapplo.Windows.SystemState --version 3.14.4
#r "nuget: Dapplo.Windows.SystemState, 3.14.4"
#:package Dapplo.Windows.SystemState@3.14.4
#addin nuget:?package=Dapplo.Windows.SystemState&version=3.14.4
#tool nuget:?package=Dapplo.Windows.SystemState&version=3.14.4
Dapplo.Windows.SystemState
Power management for .NET on Windows: keep the system awake while your application works, put it to sleep or shut it
down, wake it up with a timer, and react to suspend / resume. Targets net480 and net10.0-windows.
This package is part of Dapplo.Windows. Full documentation: Power and system state, changes: changelog.
Preventing sleep
PreventSleep keeps the system and the display on, PreventSystemSleep only the system, until the returned
SleepBlocker is disposed. It's a power request, not bound to a thread, so it works around await.
Namespace: Dapplo.Windows.SystemState.
// Keep the system and the display on until the blocker is disposed
using (SystemStateApi.PreventSleep("Recording the screen"))
{
await RecordAsync();
}
// Keep only the system awake, the display may turn off
using (SystemStateApi.PreventSystemSleep("Uploading files"))
{
await UploadAsync();
}
Sleep, shut down, log off, lock
// Sleep (suspend to RAM), applications get PBT_APMSUSPEND first
bool ok = PowerManagementApi.Sleep();
// Hibernate (suspend to disk)
ok = PowerManagementApi.Hibernate();
// Sleep, and don't let wake timers wake the system
ok = PowerManagementApi.Sleep(disableWakeEvent: true);
Shutdown and Restart enable the shutdown privilege and log a planned shutdown. Without force hung applications
are ended after a timeout; force: true closes everything without asking.
// Log off the current user
PowerManagementApi.LogOff();
// Power off; applications which don't respond are terminated after a timeout
if (!PowerManagementApi.Shutdown())
{
// e.g. 1314 (ERROR_PRIVILEGE_NOT_HELD) when the user may not shut down
Console.WriteLine($"Shutdown failed: {System.Runtime.InteropServices.Marshal.GetLastWin32Error()}");
}
// Restart
PowerManagementApi.Restart();
// Close all applications without asking them, they can lose data
PowerManagementApi.Shutdown(force: true);
// The same as Win+L. true means the lock was started, the screen might not be locked yet.
bool started = PowerManagementApi.LockWorkStation();
Wake timers
WaitableTimer can wake the PC from sleep or hibernation (the power option "Allow wake timers" must be enabled; no
privilege is needed).
using var timer = new WaitableTimer();
// Wake the PC from sleep or hibernation in 2 hours.
// No privilege is needed, but the "Allow wake timers" power setting must be enabled.
if (!timer.SetOnce(TimeSpan.FromHours(2), wakeSystem: true))
{
Console.WriteLine("Could not set the timer");
return;
}
// After the wake-up Windows broadcasts PBT_APMRESUMEAUTOMATIC, and the wait returns
timer.Wait(TimeSpan.FromHours(3));
using var timer = new WaitableTimer();
// First after 1 second, then every 5000 milliseconds
timer.SetPeriodic(initialDelay: TimeSpan.FromSeconds(1), period: 5000);
for (var i = 0; i < 5; i++)
{
timer.Wait(TimeSpan.FromSeconds(10));
Console.WriteLine($"Tick {i + 1}");
}
timer.Cancel();
Power events
PowerBroadcastListener reports WM_POWERBROADCAST (through the SharedMessageWindow of Dapplo.Windows.Messages).
Events arrive on a background thread while Windows waits: save state synchronously when suspending.
Namespaces: System.Reactive.Linq, Dapplo.Windows.SystemState.Enums.
// Called on the SharedMessageWindow thread while Windows waits (about 2 seconds): save your state synchronously
var suspendSubscription = PowerBroadcastListener.Suspending
.Subscribe(_ => SaveState());
// The user is back (opened the lid, pressed a key); this is the moment to reconnect
var resumeSubscription = PowerBroadcastListener.ResumedFromSuspend
.Subscribe(_ => Reconnect());
// Stop listening
suspendSubscription.Dispose();
resumeSubscription.Dispose();
var subscription = PowerBroadcastListener.PowerEvents
.Subscribe(powerEvent =>
{
switch (powerEvent)
{
case PowerBroadcastEvent.PBT_APMSUSPEND:
Console.WriteLine("Suspending");
break;
case PowerBroadcastEvent.PBT_APMRESUMEAUTOMATIC:
Console.WriteLine("Resumed");
break;
case PowerBroadcastEvent.PBT_APMRESUMESUSPEND:
Console.WriteLine("Resumed, the user is present");
break;
case PowerBroadcastEvent.PBT_APMPOWERSTATUSCHANGE:
Console.WriteLine("Power source or battery level changed");
break;
}
});
PowerBroadcastEvent |
When |
|---|---|
PBT_APMSUSPEND |
the system is about to suspend |
PBT_APMRESUMEAUTOMATIC |
after every wake-up, the user might not be there |
PBT_APMRESUMESUSPEND |
after a wake-up by the user |
PBT_APMPOWERSTATUSCHANGE |
AC / battery switch, battery level |
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0-windows7.0 is compatible. |
| .NET Framework | net48 is compatible. net481 was computed. |
-
.NETFramework 4.8
- Dapplo.Windows.Messages (>= 3.14.4)
- System.Reactive (>= 7.0.0)
-
net10.0-windows7.0
- Dapplo.Windows.Messages (>= 3.14.4)
- System.Reactive (>= 7.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 3.14.4 | 41 | 10/11/2026 |
| 3.14.2 | 50 | 10/10/2026 |
| 3.13.5 | 43 | 10/10/2026 |
| 3.12.2 | 44 | 10/9/2026 |
| 3.11.2 | 40 | 10/9/2026 |
| 3.10.5 | 45 | 10/9/2026 |
| 3.10.3 | 50 | 10/9/2026 |
| 3.9.2 | 62 | 10/8/2026 |
| 3.8.2 | 61 | 10/8/2026 |
| 3.7.2 | 66 | 10/8/2026 |
| 3.6.8 | 61 | 10/8/2026 |
| 3.6.3 | 61 | 10/8/2026 |
| 3.5.3 | 67 | 10/8/2026 |
| 3.4.2 | 71 | 10/8/2026 |
| 3.3.5 | 69 | 10/8/2026 |
| 3.2.3 | 91 | 9/30/2026 |
| 3.1.4 | 89 | 9/30/2026 |
| 3.0.3 | 86 | 9/30/2026 |
| 3.0.0 | 89 | 9/30/2026 |