4

Testing the code bellow in a non-surface device works just fine but when I try in a surface, I only get notifications when I remove the power cable ('statusChange' powerMode is triggered). Putting the surface in SLEEP the handler it's not called ('resume' and 'suspend' powerModes are not being trigged).
Anyone knows why ? Thank you.

Surface specs:
OS Name: Microsoft Windows 10 Pro
Version: 10.0.17134 Compilação 17134
OS Manufacturer: Microsoft Corporation
System Manufacturer: Microsoft Corporation
System Model: Surface Pro
System Type: x64-based PC
System SKU: Surface_Pro_1796

Example [WPF Visual Studio Pro 2015]
MainWindow:

using System;
using System.Windows;
using Microsoft.Win32;

namespace WpfApplication2
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            // SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged);
        }

        private void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
        {
            listView1.Items.Add(string.Format("{0} : Power mode changed = {1}", DateTime.Now, e.Mode));
        }
    }
}

XAML

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication2"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <ListView x:Name="listView1" Margin="0">
            <ListView.View>
                <GridView>
                    <GridViewColumn/>
                </GridView>
            </ListView.View>
        </ListView>

    </Grid>
</Window>
yozef
  • 43
  • 4

1 Answers1

2

I am also working on such a problem. From what I've read, the Surface supports "Sleep state(Modern Standby)", or S0 low-power, and is not yet in actual sleep state (S1-3). Pressing the power button or clicking the "sleep" option from the windows menu does not enter sleep directly but enters S0 low-power instead, thus not triggering PowerModeChanged.

https://learn.microsoft.com/en-us/windows/desktop/power/system-power-states#sleep-state-modern-standby

Chen Huang
  • 80
  • 6
  • 1
    Indeed. I had to work my way around but I don't have any strong solution. You can use **SystemEvents.SessionSwitch** or **PowerManager.IsMonitorOnChanged** (comes from a nuget package called [WindowsAPICodePack](https://www.nuget.org/packages/WindowsAPICodePack-Core/) but they were not enterily precise for my case. – yozef Jul 17 '18 at 13:40
  • Other hacky solution is to mess with registry by disable the **Connected Standby/Modern Standby** by: setting the value **CsEnabled** to **0** (a restart is require to take action) at *HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power*. This will disable the sleep mode completely. Surface will instead hibernate and **PowerModeChanged** will be triggered. If you don't disable this option and try to set power button to hibernate the **PowerModeChanged** will not be triggered. *shrugs* – yozef Jul 17 '18 at 13:48
  • 1
    Code based on @yozef answer here: https://stackoverflow.com/questions/20119257/connected-standby-notification-for-a-w8-service/55676206#55676206 – Oded Ben Dov Apr 14 '19 at 14:14