문제

나는 잠자기 전에 기기가 기다릴 시간의 양을 갱신하기 위해 사용하는 코드가 있습니다.

레지스트리를 업데이트 한 다음 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);
.

i ChangeBatteryTimeout(10);로 이것을 호출하지만 레지스트리가 변경되는 동안 장치가 10 초 안에 잠자기가 없습니다.(이전에 설정된 값은 190 초가 걸립니다.)

도움이되는 모든 도움이 될 것입니다.

도움이 되었습니까?

해결책

CE 공용 소스의 제어판의 소스 코드를 살펴보십시오 (플랫폼 빌더를 지금까지 다운로드 한 것, 오른쪽?).IIRC가 변경하면 wm_settingchange 메시지.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top