Dapplo.Windows.SystemState 3.6.3

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Dapplo.Windows.SystemState --version 3.6.3
                    
NuGet\Install-Package Dapplo.Windows.SystemState -Version 3.6.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Dapplo.Windows.SystemState" Version="3.6.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Dapplo.Windows.SystemState" Version="3.6.3" />
                    
Directory.Packages.props
<PackageReference Include="Dapplo.Windows.SystemState" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Dapplo.Windows.SystemState --version 3.6.3
                    
#r "nuget: Dapplo.Windows.SystemState, 3.6.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Dapplo.Windows.SystemState@3.6.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Dapplo.Windows.SystemState&version=3.6.3
                    
Install as a Cake Addin
#tool nuget:?package=Dapplo.Windows.SystemState&version=3.6.3
                    
Install as a Cake Tool

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 Compatible and additional computed target framework versions.
.NET net10.0-windows7.0 is compatible. 
.NET Framework net48 is compatible.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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 49 10/11/2026
3.14.2 57 10/10/2026
3.13.5 51 10/10/2026
3.12.2 51 10/9/2026
3.11.2 46 10/9/2026
3.10.5 52 10/9/2026
3.10.3 57 10/9/2026
3.9.2 68 10/8/2026
3.8.2 67 10/8/2026
3.7.2 73 10/8/2026
3.6.8 67 10/8/2026
3.6.3 68 10/8/2026
3.5.3 74 10/8/2026
3.4.2 78 10/8/2026
3.3.5 76 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