WindowsAppSDK
WindowsAppSDK copied to clipboard
PowerManager SystemSuspendStatusChanged unusable
Describe the bug
This minimal example, which involves registering for an event and checking which kind was triggered, doesn't work:
PowerManager::SystemSuspendStatusChanged([](auto const&, auto const&) {
const auto status = PowerManager::SystemSuspendStatus();
});
This generates: API only callable after a SystemSuspendStatusChanged callback
.
The following code only makes the status known during the event, but RaiseEvent()
is a coroutine that runs the callback in a background thread, making m_systemSuspendStatus
be SystemSuspendStatus::Uninitialized
by the time the callback is called.
https://github.com/microsoft/WindowsAppSDK/blob/5d49c0dc48243fa86c257019ece44c1f3d09b6af/dev/PowerNotifications/PowerNotifications.h#L618-L638
Steps to reproduce the bug
- Create a packaged blank UWP C++ app, add the code above to
App::OnLaunched
. - Run app.
- Sleep and wake pc.
- Exception is thrown.
Expected behavior
PowerManager::SystemSuspendStatus()
callable.
Screenshots
No response
NuGet package version
1.1.3
Packaging type
Packaged (MSIX)
Windows version
Windows 10 version 21H2 (19044, November 2021 Update)
IDE
Visual Studio 2022
Additional context
No response
Does this reproduce in a Win32 packaged or unpackaged application?
@brialmsft or @kanismohammed can you check?
Still seeing this issue in a packaged WinAppSDK 1.2 application. I didn't try an unpackaged variant.
The PowerManager code appears to be broken. From the code, I wouldn't expect the event to ever fire or for getting the property value to ever work. Logged (internal bug) for this issue.
A possible workaround currently would be for an app to directly use PowerRegisterSuspendResumeNotification function (powerbase.h) - Win32 apps | Microsoft Learn to listen for changes. That is easier for a C++ app than a C# app.
If it helps, back when I reported the issue in 2022 the event was indeed firing correctly for me. The issue was with the supporting call SystemSuspendStatus()
, which would be essential to use this API, is unusable due to it always failing.
The description has a possible reason why I think this is broken, which is that the code thinks RaiseEvent()
is synchronous, due to the comment left there // Resetting the value after the callback
, when in fact it isn't.