Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SystemEvents.PowerModeChanged not fireing if computer is put to sleep over start menu #78162

Open
vsfeedback opened this issue Nov 10, 2022 · 4 comments
Labels
area-Microsoft.Win32 bug tracking-external-issue The issue is caused by external problem (e.g. OS) - nothing we can do to fix it directly
Milestone

Comments

@vsfeedback
Copy link

This issue has been moved from a ticket on Developer Community.


[severity:It bothers me. A fix would be nice]
I am using the following code in my application:

SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(OnPoweModerChange);

static void OnPoweModerChange(object s, PowerModeChangedEventArgs e)
        {
            SimpleLog.Info("OnPoweModerChange");
            if (e.Mode == PowerModes.Suspend)
            {
                SimpleLog.Info("Suspend");
            }
            else if (e.Mode == PowerModes.Resume)
            {
                SimpleLog.Info("Resume");
            }
            else
            {
                try
                {
                    SimpleLog.Info(e.Mode.ToString());
                }
                catch (Exception ex)
                {
                    SimpleLog.Warning(ex.ToString());
                }
            }
        }

It should tell weather the system has been suspended or not. However this is only the case if the system goes to sleep by itself. This can be set in the system settings.

If I put the system to sleep manually. The PowerModeChanged event is not fired at all.

Is there another Function to catch that or is this an error in windows?

I do not remember it being the case with windows 10.
It is definitely a Problem on Windows 11!
image.png


Original Comments

Feedback Bot on 10/28/2022, 04:40 AM:

(private comment, text removed)


Original Solutions

(no solutions)

@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Nov 10, 2022
@ghost
Copy link

ghost commented Nov 10, 2022

Tagging subscribers to this area: @dotnet/area-microsoft-win32
See info in area-owners.md if you want to be subscribed.

Issue Details

This issue has been moved from a ticket on Developer Community.


[severity:It bothers me. A fix would be nice]
I am using the following code in my application:

SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(OnPoweModerChange);

static void OnPoweModerChange(object s, PowerModeChangedEventArgs e)
        {
            SimpleLog.Info("OnPoweModerChange");
            if (e.Mode == PowerModes.Suspend)
            {
                SimpleLog.Info("Suspend");
            }
            else if (e.Mode == PowerModes.Resume)
            {
                SimpleLog.Info("Resume");
            }
            else
            {
                try
                {
                    SimpleLog.Info(e.Mode.ToString());
                }
                catch (Exception ex)
                {
                    SimpleLog.Warning(ex.ToString());
                }
            }
        }

It should tell weather the system has been suspended or not. However this is only the case if the system goes to sleep by itself. This can be set in the system settings.

If I put the system to sleep manually. The PowerModeChanged event is not fired at all.

Is there another Function to catch that or is this an error in windows?

I do not remember it being the case with windows 10.
It is definitely a Problem on Windows 11!
image.png


Original Comments

Feedback Bot on 10/28/2022, 04:40 AM:

(private comment, text removed)


Original Solutions

(no solutions)

Author: vsfeedback
Assignees: -
Labels:

area-Microsoft.Win32, untriaged

Milestone: -

@ViktorHofer ViktorHofer added this to the 8.0.0 milestone Jun 28, 2023
@ViktorHofer ViktorHofer removed the untriaged New issue has not been triaged by the area owner label Jun 28, 2023
@ericstj ericstj added untriaged New issue has not been triaged by the area owner and removed untriaged New issue has not been triaged by the area owner labels Jul 20, 2023
@carlossanlop
Copy link
Member

Mislav Boras (original submitter in VS Feedback) hopefully you see this message: what .NET version did you use to test this?

@carlossanlop
Copy link
Member

I wrote a small test console app and was able to repro with .NET 6 and .NET 8 in Windows 11:

  • The PowerMode.Resume and PowerModes.Suspend do not get triggered after putting the computer to sleep and waking it up again.
  • But if I disconnect the battery charger, the PowerMode.StatusChange event triggers and gets correctly read by the registered PowerModeChangedEventHandler, meaning our code is doing the right thing.

Here's the app:

using Microsoft.Win32;
using System;

namespace ConsoleApp;

public class Program
{
    public static void Main()
    {
        SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(OnPowerModeChange);
        Console.WriteLine("Running...");
        Console.ReadLine();
    }

    private static void OnPowerModeChange(object s, PowerModeChangedEventArgs e)
    {
        Log("OnPowerModeChange");
        try
        {
            Log(e.Mode.ToString());
            
        }
        catch (Exception ex)
        {
            Log($"Exception: {ex.Message}\n" + $"Stacktrace:\n{ex.StackTrace}\n\n");
        }
    }

    private static void Log(string content) => Console.WriteLine($"{DateTime.Now} : {content}");
}

Our unit tests pass by manually registering the Windows messages and detecting them in these APIs:

[ConditionalTheory(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWindowsNanoNorServerCore))]
[InlineData(User32.PBT_APMBATTERYLOW, PowerModes.StatusChange)]
[InlineData(User32.PBT_APMOEMEVENT, PowerModes.StatusChange)]
[InlineData(User32.PBT_APMPOWERSTATUSCHANGE, PowerModes.StatusChange)]
[InlineData(User32.PBT_APMRESUMECRITICAL, PowerModes.Resume)]
[InlineData(User32.PBT_APMRESUMESUSPEND, PowerModes.Resume)]
[InlineData(User32.PBT_APMRESUMESTANDBY, PowerModes.Resume)]
[InlineData(User32.PBT_APMSUSPEND, PowerModes.Suspend)]
[InlineData(User32.PBT_APMSTANDBY, PowerModes.Suspend)]
public void SignalsPowerModeChanged(int pmEvent, PowerModes powerMode)
{
bool changed = false;
PowerModeChangedEventArgs args = null;
PowerModeChangedEventHandler changedHandler = (o, e) =>
{
changed = true;
args = e;
};
SystemEvents.PowerModeChanged += changedHandler;
try
{
SendMessage(pmEvent);
Assert.True(changed);
Assert.NotNull(args);
Assert.Equal(powerMode, args.Mode);
}
finally
{
SystemEvents.PowerModeChanged -= changedHandler;
}
}

So this looks a lot like a Windows 11 issue.

@carlossanlop carlossanlop modified the milestones: 8.0.0, Future Jul 26, 2023
@carlossanlop carlossanlop added the tracking-external-issue The issue is caused by external problem (e.g. OS) - nothing we can do to fix it directly label Jul 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Microsoft.Win32 bug tracking-external-issue The issue is caused by external problem (e.g. OS) - nothing we can do to fix it directly
Projects
None yet
Development

No branches or pull requests

5 participants