レジストリから電力タイムアウト値をリロードするようにOSに指示する
-
15-09-2020 - |
質問
私は、デバイスが眠る前に待機する時間を更新するために使用しているコードをいくつか持っています。
レジストリを更新してから、値をリロードするようにOSに指示してみてくださいが、機能していません。(リロード部分が機能しません。レジストリが大丈夫です。)
私は誰かが私が間違っていることを知っていることを希望に投稿しようとしています:
// 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 Public Sourceのコントロールパネルのソースコードを調べてください(Platform Builderを今すぐダウンロードしました。右?)。IIRCあなたが変更をするとき、それがブロードキャストされたとき wm_settingchange メッセージ。
所属していません StackOverflow