WindowsAppSDK icon indicating copy to clipboard operation
WindowsAppSDK copied to clipboard

PowerManager SystemSuspendStatusChanged unusable

Open javs opened this issue 2 years ago • 4 comments

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

  1. Create a packaged blank UWP C++ app, add the code above to App::OnLaunched.
  2. Run app.
  3. Sleep and wake pc.
  4. 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

javs avatar Aug 08 '22 22:08 javs

Does this reproduce in a Win32 packaged or unpackaged application?

@brialmsft or @kanismohammed can you check?

jonwis avatar Aug 08 '22 23:08 jonwis

Still seeing this issue in a packaged WinAppSDK 1.2 application. I didn't try an unpackaged variant.

brentafis avatar Dec 13 '22 15:12 brentafis

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.

codendone avatar Nov 16 '23 21:11 codendone

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.

javs avatar Nov 16 '23 22:11 javs