【发布时间】:2014-06-17 09:57:33
【问题描述】:
我需要禁用connected standby 模式,直到我的桌面应用程序完成。所需的行为应该类似于我通过远程桌面连接到该机器时发生的情况。也就是说,屏幕已关闭,但系统在我断开连接之前不会进入睡眠状态。
是否有任何记录或未记录的方式可以让我的应用程序获得相同的行为?
我尝试使用PowerSetRequest 和PowerRequestExecutionRequired 和/或PowerRequestAwayModeRequired,但系统在几分钟后仍会进入连接待机模式。我目前使用PowerRequestDisplayRequired 使其保持活动状态,但屏幕始终亮着。
已编辑。这是测试应用程序。在我按下硬件电源按钮并且屏幕关闭(依靠电池运行)后,计时器的计时不超过 5 分钟。
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace CsTestApp
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
this.Load += MainForm_Load;
}
void MainForm_Load(object sender, EventArgs e)
{
// init timer
var timer = new System.Windows.Forms.Timer();
timer.Interval = 1000;
timer.Tick += delegate
{
System.Diagnostics.Trace.WriteLine("CsTestApp: " + DateTime.Now);
};
timer.Start();
// set GUID_EXECUTION_REQUIRED_REQUEST_TIMEOUT
IntPtr pActiveSchemeGuid;
var hr = PowerGetActiveScheme(IntPtr.Zero, out pActiveSchemeGuid);
if (hr != 0)
Marshal.ThrowExceptionForHR((int)hr);
Guid activeSchemeGuid = (Guid)Marshal.PtrToStructure(pActiveSchemeGuid, typeof(Guid));
LocalFree(pActiveSchemeGuid);
int savedTimeout;
hr = PowerReadDCValueIndex(
IntPtr.Zero,
activeSchemeGuid,
GUID_IDLE_RESILIENCY_SUBGROUP,
GUID_EXECUTION_REQUIRED_REQUEST_TIMEOUT,
out savedTimeout);
if (hr != 0)
Marshal.ThrowExceptionForHR((int)hr);
hr = PowerWriteDCValueIndex(
IntPtr.Zero,
activeSchemeGuid,
GUID_IDLE_RESILIENCY_SUBGROUP,
GUID_EXECUTION_REQUIRED_REQUEST_TIMEOUT,
-1);
if (hr != 0)
Marshal.ThrowExceptionForHR((int)hr);
// create power request
var powerRequestContext = new POWER_REQUEST_CONTEXT();
powerRequestContext.Version = POWER_REQUEST_CONTEXT_VERSION;
powerRequestContext.Flags = POWER_REQUEST_CONTEXT_SIMPLE_STRING;
powerRequestContext.SimpleReasonString = "Disable Connected Standby";
var powerRequest = PowerCreateRequest(ref powerRequestContext);
if (powerRequest == IntPtr.Zero)
ThrowLastWin32Error();
// set PowerRequestExecutionRequired
if (!PowerSetRequest(powerRequest, PowerRequestType.PowerRequestExecutionRequired))
ThrowLastWin32Error();
this.FormClosed += delegate
{
timer.Dispose();
PowerClearRequest(powerRequest, PowerRequestType.PowerRequestExecutionRequired);
CloseHandle(powerRequest);
hr = PowerWriteDCValueIndex(
IntPtr.Zero,
activeSchemeGuid,
GUID_IDLE_RESILIENCY_SUBGROUP,
GUID_EXECUTION_REQUIRED_REQUEST_TIMEOUT,
savedTimeout);
if (hr != 0)
Marshal.ThrowExceptionForHR((int)hr);
};
}
// power API interop
static void ThrowLastWin32Error()
{
throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
}
enum PowerRequestType
{
PowerRequestDisplayRequired = 0,
PowerRequestSystemRequired = 1,
PowerRequestAwayModeRequired = 2,
PowerRequestExecutionRequired = 3,
PowerRequestMaximum
}
[StructLayout(LayoutKind.Sequential)]
struct PowerRequestContextDetailedInformation
{
public IntPtr LocalizedReasonModule;
public UInt32 LocalizedReasonId;
public UInt32 ReasonStringCount;
[MarshalAs(UnmanagedType.LPWStr)]
public string[] ReasonStrings;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
struct POWER_REQUEST_CONTEXT_DETAILED
{
public UInt32 Version;
public UInt32 Flags;
public PowerRequestContextDetailedInformation DetailedInformation;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
struct POWER_REQUEST_CONTEXT
{
public UInt32 Version;
public UInt32 Flags;
[MarshalAs(UnmanagedType.LPWStr)]
public string SimpleReasonString;
}
const int POWER_REQUEST_CONTEXT_VERSION = 0;
const int POWER_REQUEST_CONTEXT_SIMPLE_STRING = 0x1;
const int POWER_REQUEST_CONTEXT_DETAILED_STRING = 0x2;
static readonly Guid GUID_IDLE_RESILIENCY_SUBGROUP = new Guid(0x2e601130, 0x5351, 0x4d9d, 0x8e, 0x4, 0x25, 0x29, 0x66, 0xba, 0xd0, 0x54);
static readonly Guid GUID_EXECUTION_REQUIRED_REQUEST_TIMEOUT = new Guid(0x3166bc41, 0x7e98, 0x4e03, 0xb3, 0x4e, 0xec, 0xf, 0x5f, 0x2b, 0x21, 0x8e);
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr PowerCreateRequest(ref POWER_REQUEST_CONTEXT Context);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool PowerSetRequest(IntPtr PowerRequestHandle, PowerRequestType RequestType);
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool PowerClearRequest(IntPtr PowerRequestHandle, PowerRequestType RequestType);
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr LocalFree(IntPtr hMem);
[DllImport("PowrProf.dll", CharSet = CharSet.Unicode)]
static extern UInt32 PowerWriteDCValueIndex(IntPtr RootPowerKey,
[MarshalAs(UnmanagedType.LPStruct)] Guid SchemeGuid,
[MarshalAs(UnmanagedType.LPStruct)] Guid SubGroupOfPowerSettingsGuid,
[MarshalAs(UnmanagedType.LPStruct)] Guid PowerSettingGuid,
int AcValueIndex);
[DllImport("PowrProf.dll", CharSet = CharSet.Unicode)]
static extern UInt32 PowerReadDCValueIndex(IntPtr RootPowerKey,
[MarshalAs(UnmanagedType.LPStruct)] Guid SchemeGuid,
[MarshalAs(UnmanagedType.LPStruct)] Guid SubGroupOfPowerSettingsGuid,
[MarshalAs(UnmanagedType.LPStruct)] Guid PowerSettingGuid,
out int AcValueIndex);
[DllImport("PowrProf.dll", CharSet = CharSet.Unicode)]
static extern UInt32 PowerGetActiveScheme(IntPtr UserPowerKey, out IntPtr ActivePolicyGuid);
}
}
这是powercfg.exe /requests的输出:
【问题讨论】:
-
@EricBrown,是的,我在
PowerSetRequest之前先尝试过,但没有帮助。 -
@EricBrown,这是一个 8" ASUS VivoTab Note 8,这个使用 Intel Z3740 CPU。我认为它对于任何 Bay Trail SoC 设备都是一样的,因为驱动程序几乎相同并且由英特尔提供。
-
查看我可用的资源,大多数地方只是使用
PowerRequestExecutionRequired,然后执行正常活动(通常使用网络/文件系统)。有几个地方同时使用PowerRequestExecutionRequired和PowerRequestDisplayRequired;从当地情况来看,它是否真的在显示某些东西(例如播放媒体)并不清楚。 -
@EricBrown,它出现在
PowerRequestExecutionRequiredis limited to 5 mins only。
标签: c# c++ .net windows winapi