Dígale al sistema operativo que vuelva a cargar los valores de tiempo de espera de energía del registro

StackOverflow https://stackoverflow.com/questions/3250929

Pregunta

Tengo algún código que estoy usando para actualizar la cantidad de tiempo que el dispositivo esperará antes de irse a dormir.

Actualizo el registro y luego intento decirle al sistema operativo que vuelva a cargar el valor, pero no funciona.(La parte de recarga no funciona. El registro se actualiza bien.)

Voy a publicar mi código con las esperanzas de que alguien sepa lo que estoy haciendo mal:

// 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);

Llamo a esto con ChangeBatteryTimeout(10);, pero, mientras cambia el registro, el dispositivo no duerme en 10 segundos.(Se necesita el valor establecido anteriormente de 190 segundos).

Se apreciaría cualquier ayuda.

¿Fue útil?

Solución

Eche un vistazo al código fuente para el panel de control en la fuente pública CE (ya ha descargado Platform Builder por ahora, ¿verdad?).IIRC Cuando realiza el cambio, transmite una wm_settingchange Mensaje.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top