public interface IDesktopPowerEvents : IAsyncDisposable Sleep, wake, session lock and battery events of the computer.
Remarks
An application-scoped service that needs no window; a ViewModel takes it in its constructor. Results and watch items arrive on the thread pool. Dispose it at shutdown; that ends its watches.
Windows: WM_POWERBROADCAST, WTSRegisterSessionNotification, GetSystemPowerStatus and the power saving status. macOS: NSWorkspace's sleep and wake notifications, the screen lock notifications, IOKit's power source and NSProcessInfo's Low Power Mode. Linux: logind's PrepareForSleep and the session's Lock, Unlock and LockedHint, UPower's OnBattery and the power-saver profile, on the system bus; inside Flatpak that needs --system-talk-name for org.freedesktop.login1, org.freedesktop.UPower and org.freedesktop.UPower.PowerProfiles. Hosts without a desktop, such as CS-WebUI and serve-only Runic Desktop, report Unavailable.
Methods
ReadAsync
ValueTask<PlatformResult<DesktopPowerState>> ReadAsync(CancellationToken cancellationToken = default) Reads the current power and session state.
Parameters
cancellationTokenCancels waiting for the answer.
Returns
The state, or Unavailable when the platform has no source for it.
Exceptions
ObjectDisposedExceptionThe service was disposed.
WatchAsync
IAsyncEnumerable<PlatformResult<DesktopPowerEvent>> WatchAsync(CancellationToken cancellationToken = default) Reports the current state as DesktopPowerEventKind.StateChanged, then each event.
Parameters
cancellationTokenEnds the watch, which then throws
OperationCanceledException.
Exceptions
ObjectDisposedExceptionThe service was disposed.
Remarks
A DesktopPowerEventKind.StateChanged that leaves the state as it was is not reported, nor is a repeated event, such as a second DesktopPowerEventKind.Locked. A reader that falls behind loses no DesktopPowerEventKind.Suspending, DesktopPowerEventKind.Resumed, DesktopPowerEventKind.Locked or DesktopPowerEventKind.Unlocked; state changes waiting between two of them are combined into the latest. When a source goes away the watch reports Unavailable and continues with the current state once it is back, so events while it was away, such as a sleep and wake, are missed and only the state after them shows; without a source it reports Unavailable once and then waits. Disposing the service ends the watch.