我有一些代码用于更新设备在进入睡眠状态之前等待的时间。
我更新了注册表,然后尝试告诉操作系统重新加载该值,但它无法正常工作。 (重新加载部分不起作用。注册表更新正常。)
我将发布我的代码,希望有人知道我做错了什么:
// Change the amount of seconds that the OS will wait before it goes to sleep.
public void ChangeBatteryTimeout(int timeoutInSeconds)
{
// Attempt to open the key
RegistryKey key = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Power\\Timeouts", true) ??
// If the return value is null, the key doesn't exist, so create it.
Registry.LocalMachine.CreateSubKey("SYSTEM\\CurrentControlSet\\Control\\Power\\Timeouts");
if (key == null)
throw new KeyNotFoundException("Could not find or make the key for timeout manangment.");
// This value must be set for the timeout stuff to work.
// See:http://msdn.microsoft.com/en-us/library/aa932196.aspx
if (key.GetValue("ACUserIdle") == null)
key.SetValue("ACUserIdle", 0, RegistryValueKind.DWord);
// Set the Battery Suspend Timeout to be the passed in value.
key.SetValue("BattSuspendTimeout", timeoutInSeconds, RegistryValueKind.DWord);
// Signal the OS to reload the value we just changed.
DoAutoResetEvent();
}
// Tell to OS to reload the timeouts.
private static void DoAutoResetEvent()
{
const string eventString = "PowerManager/ReloadActivityTimeouts";
IntPtr newHandle = CreateEvent(IntPtr.Zero, false, false, eventString);
EventModify(newHandle, (int)EventFlags.EVENT_SET);
CloseHandle(newHandle);
}
private enum EventFlags
{
EVENT_PULSE = 1,EVENT_RESET = 2,EVENT_SET = 3
}
[DllImport("coredll.dll", SetLastError = true)]
private static extern IntPtr CreateEvent(IntPtr lpEventAttributes,
bool bManualReset, bool bInitialState, string lpName);
[DllImport("coredll")]
static extern bool EventModify(IntPtr hEvent, int func);
[DllImport("coredll.dll", SetLastError = true)]
private static extern bool CloseHandle(IntPtr hObject);
我用ChangeBatteryTimeout(10);
调用它,但是,当注册表更改时,设备不会在10秒内休眠。 (它需要先前设定的值190秒。)
任何帮助都将不胜感激。
答案 0 :(得分:1)
查看CE公共源代码中控制面板的源代码(您现在已经下载了Platform Builder,对吧?)。 IIRC在您进行更改时会广播WM_SETTINGCHANGE消息。