Gar*_*ett 6 c# hibernate-mode sleep-mode
我的应用程序需要阻止睡眠/休眠模式.我已经有了代码,但在成功捕获WM_POWERBROADCAST消息后,PBT_APMQUERYSUSPEND和PBT_APMQUERYSTANDBY都未成功捕获.有趣的是,PBT_APMRESUMECRITICAL和PBT_APMRESUMEAUTOMATIC消息都被我的应用程序捕获.
底线问题:我的应用程序无法捕获备用/暂停消息,但成功捕获恢复消息是否有任何原因?
这个Q&A [stackoverflow.com]有帮助,顺便说一下,但是这些消息似乎没有进入我的应用程序.
我的代码(为了简洁起见删除了事件日志代码):
protected override void WndProc(ref System.Windows.Forms.Message m)
{
// Power status event triggered
if (m.Msg == (int)NativeMethods.WindowMessage.WM_POWERBROADCAST)
{
// Machine is trying to enter suspended state
if (m.WParam.ToInt32() == (int)NativeMethods.WindowMessage.PBT_APMQUERYSUSPEND ||
m.WParam.ToInt32() == (int)NativeMethods.WindowMessage.PBT_APMQUERYSTANDBY)
{
// Have perms to deny this message?
if((m.LParam.ToInt32() & 0x1) != 0)
{
// If so, deny broadcast message
m.Result = new IntPtr((int)NativeMethods.WindowMessage.BROADCAST_QUERY_DENY);
}
}
return; // ?!
}
base.WndProc(ref m);
}
Run Code Online (Sandbox Code Playgroud)
现在它适用于 XP 和 Vista。我使用相关代码创建了一个存根 winform 应用程序(显然可以清理,但它传达了要点)。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace standbyTest
{
public partial class Form1 : Form
{
[DllImport("Kernel32.DLL", CharSet = CharSet.Auto, SetLastError = true)]
protected static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE state);
[Flags]
public enum EXECUTION_STATE : uint
{
ES_CONTINUOUS = 0x80000000,
ES_DISPLAY_REQUIRED = 2,
ES_SYSTEM_REQUIRED = 1,
ES_AWAYMODE_REQUIRED = 0x00000040
}
public Form1()
{
if(Environment.OSVersion.Version.Major > 5)
{
// vista and above: block suspend mode
SetThreadExecutionState(EXECUTION_STATE.ES_AWAYMODE_REQUIRED | EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS);
}
InitializeComponent();
//MessageBox.Show(string.Format("version: {0}", Environment.OSVersion.Version.Major.ToString() ));
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
if(Environment.OSVersion.Version.Major > 5)
{
// Re-allow suspend mode
SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);
}
}
protected override void WndProc(ref System.Windows.Forms.Message m)
{
// Power status event triggered
if(m.Msg == (int)WindowMessage.WM_POWERBROADCAST)
{
// Machine is trying to enter suspended state
if(m.WParam.ToInt32() == (int)WindowMessage.PBT_APMQUERYSUSPEND ||
m.WParam.ToInt32() == (int)WindowMessage.PBT_APMQUERYSTANDBY)
{
// Have perms to deny this message?
if((m.LParam.ToInt32() & 0x1) != 0)
{
// If so, deny broadcast message
m.Result = new IntPtr((int)WindowMessage.BROADCAST_QUERY_DENY);
}
}
return;
}
base.WndProc(ref m);
}
}
internal enum WindowMessage
{
/// <summary>
/// Notify that machine power state is changing
/// </summary>
WM_POWERBROADCAST = 0x218,
/// <summary>
/// Message indicating that machine is trying to enter suspended state
/// </summary>
PBT_APMQUERYSUSPEND = 0x0,
PBT_APMQUERYSTANDBY = 0x0001,
/// <summary>
/// Message to deny broadcast query
/// </summary>
BROADCAST_QUERY_DENY = 0x424D5144
}
}
Run Code Online (Sandbox Code Playgroud)