当计算机从睡眠/休眠模式恢复时,如何捕捉事件



我有一个控制台应用程序运行在.net 4.5上(仅限(。我正在尝试检测计算机何时从睡眠/休眠模式返回。我试过使用Win32.SystemEvents.PowerModeChanged,但由于某种原因,它不起作用。。。我使用的是运行windows 10的ThinkPad笔记本电脑,当我拔下充电电缆时,它确实会触发参数为Mode=PowerModes.StatusChange的事件。

class Program
{
static void Main(string[] args)
{
SystemEvents.PowerModeChanged += new PowerModeChangedEventHandler(SystemEvents_PowerModeChanged);
Console.WriteLine("This application is waiting for system events.");
Console.WriteLine("Press <Enter> to terminate this application.");
Console.ReadLine();
}
private static void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
Console.WriteLine(Enum.GetName(typeof(PowerModes), e.Mode));
File.WriteAllText("test.txt", "test");
}
}

尝试打印到屏幕并写入文件,但无法使其工作。。。

如果有人有想法或不同的方法,最后我需要了解电脑何时从睡眠或休眠状态返回。

已解决:

在windows 10中,微软添加了Modern Standby,扩展了windows 8.1 Connected Standby电源模式。net 4.5中的SystemEvents.PowerModeChanged仅支持传统睡眠和休眠(S1-4(。

从windows 10,版本2004现代待机是强制性的,不能被禁用,使SystemEvents.PowerModeChanged在我的情况下毫无用处。

此处引用了用于处理现代待机电源模式更改的新Win32 API:PowerRegisterSuspendResultNotification函数MSDN

遗憾的是,我没能为新的API找到一个完整的C#实现。

Soo我自己用来自Vanara Project By dahall(GitHub(的User32.dllPowrPorf.dll的C#包装器做了一个:

public static class SystemPowerNotifications
{
public static event SystemPowerNotificationEventHandler PowerModeChanged 
{ 
add
{                           
_powerModeChanged += value;
if (_eventHandler == null)
{
var result = PowrProf.PowerRegisterSuspendResumeNotification(PowrProf.RegisterSuspendResumeNotificationFlags.DEVICE_NOTIFY_CALLBACK,
_dnsp, out _eventHandler);
if (result != Win32Error.ERROR_SUCCESS)
throw new Exception();
}
} 
remove 
{
_powerModeChanged -= value;
if(_powerModeChanged.GetInvocationList().Length == 0)
{
if (PowrProf.PowerUnregisterSuspendResumeNotification(_eventHandler) != Win32Error.NO_ERROR)
throw new Exception();
_eventHandler.Dispose();
_eventHandler = null;
}       
}
}
private static PowrProf.SafeHPOWERNOTIFY _eventHandler;
private static PowrProf.DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS _dnsp = new PowrProf.DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS
{
Callback = OnDeviceNotify,
Context = IntPtr.Zero
};
private static Win32Error OnDeviceNotify(IntPtr context, uint type, IntPtr setting)
{
_powerModeChanged?.Invoke(null,new PowerNotificationArgs((PowerBroadcastType)type));
return 0;
}
private static SystemPowerNotificationEventHandler _powerModeChanged;
}

完整源代码:SystemPowerModeNotification-dotnet4.5(GitHub(

编辑:

在Windows服务中使用时,PowerRegisterSuspendResumeNotification中注册的回调函数仅在进入休眠模式时才会启动,而在现代待机睡眠/监视器关闭时不会启动

您需要注册到另一个名为RegisterPowerSettingNotification的通知,此处引用:注册Power Events MSDN以及当CCD_ 9检查监视器状态时。请记住,即使计算机在未进入睡眠模式的情况下进入监视器关闭/打开状态,也会发生这种情况。

注册示例:

public static event SystemPowerNotificationEventHandler PowerModeChanged
{
add
{
_powerModeChanged += value;
if (_powerEventHandler == null)
{
if (!string.IsNullOrEmpty(ServiceName))
{
if (_ssh.IsNull)
_ssh = AdvApi32.RegisterServiceCtrlHandlerEx(ServiceName, OnDisplayNotify);
if (_ssh.IsNull)
throw new Exception("Failed To Register ServiceCtrlHandlerEx");
_displayEventHandler = User32.RegisterPowerSettingNotification(((IntPtr)_ssh), PowrProf.GUID_MONITOR_POWER_ON, User32.DEVICE_NOTIFY.DEVICE_NOTIFY_SERVICE_HANDLE);
if (_displayEventHandler.IsNull)
throw new Exception("Failed To Register PowerSettingNotification");
}
var result = PowrProf.PowerRegisterSuspendResumeNotification(PowrProf.RegisterSuspendResumeNotificationFlags.DEVICE_NOTIFY_CALLBACK,
_dnsp, out _powerEventHandler);
if (result != Win32Error.ERROR_SUCCESS)
throw new Exception("Failed To Register PowerSuspendResumeNotification");
}
}
remove
{
_powerModeChanged -= value;
if (_powerModeChanged == null)
{
if (!string.IsNullOrEmpty(ServiceName))
{
if (!User32.UnregisterPowerSettingNotification(_displayEventHandler))
throw new Exception("Failed To Unregister PowerSettingNotification");
_displayEventHandler.Dispose();
_displayEventHandler = null;
}
if (PowrProf.PowerUnregisterSuspendResumeNotification(_powerEventHandler) != Win32Error.NO_ERROR)
throw new Exception("Failed To Unregister PowerSuspendResumeNotification");
_powerEventHandler.Dispose();
_powerEventHandler = null;
}
}
}

回调示例:

private static Win32Error OnDisplayNotify(AdvApi32.ServiceControl control,uint eventType,IntPtr eventData,IntPtr context)
{
var dataHandle = new HANDLE(eventData);
var contextHandle = new HANDLE(context);
if(control == AdvApi32.ServiceControl.SERVICE_CONTROL_POWEREVENT)
{
POWERBRODCAST_SETTING settings = (POWERBRODCAST_SETTING)Marshal.PtrToStructure(eventData, typeof(POWERBRODCAST_SETTING));
_powerModeChanged?.Invoke(null, new PowerNotificationArgs((PowerBroadcastType)eventType,settings.Data));
}

return 0;
}

相关内容

  • 没有找到相关文章

最新更新