告诉操作系统从注册表重新加载电源超时值
-
15-09-2020 - |
题
我有一些我用来更新设备在睡觉前等待的时间量。
我更新注册表,然后尝试告诉操作系统重新加载值,但它不起作用。(重新加载部分不起作用。注册表更新Fine。)
我要发布我的代码,希望有人知道我做错了什么:
// 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秒的预先设定值。)
任何帮助将是值得注意的。
解决方案
看看CE公共来源中控制面板的源代码(您现在已下载了平台制造商,对右?)。IIRC当您将更改进行更改时,它播放一个 wm_settingchange 消息。
不隶属于 StackOverflow