views:

20

answers:

1

I have some code that I am using to update the amount of time the device will wait before going to sleep.

I update the registry and then try to tell the OS to reload the value, but it is not working. (The reload part does not work. The registry updates fine.)

I am going to post my code in the hopes that someone knows what I am doing wrong:

// 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 call this with ChangeBatteryTimeout(10); but, while the registry changes, the device does not sleep in 10 seconds. (It takes the previously set value of 190 seconds.)

Any help would be appreciated.

+1  A: 

Take a look at the source code for the Control Panel in the CE public source (you have downloaded Platform Builder by now, right?). IIRC when you make the change it broadcasts a WM_SETTINGCHANGE message.

ctacke
I did have platform builder installed. But a recent hard drive crash has left me with out it again. I am installing now.... I will look into sending out the WM_SETTINGCHANGE message. Thanks again ctacke! You are the guru of all things mobile!
Vaccano
I looked at the source for Control Panel, but I am not seeing the broadcast of WM_SETTINGCHANGED for a battery related activity (I only see one for updating the back ground.) I would expect to see something using SPI_SETBATTERYIDLETIMEOUT or something similar. Could you point me to the file you are looking at?
Vaccano
The link is to the full OS version (Windows XP/whatever). This is the link to Windows Mobile: http://msdn.microsoft.com/en-us/library/aa931802.aspx
Shaihi