0

We are using C# .Net Compact Edition 3.5 with Windows Mobile 6.1 and not very familiar with C++ or Windows API calls. We need to programmatically change the battery idle/suspect time from whatever it's set (usually defaults to 3/5 minutes) to 15 minutes. I've found some examples online, but so far none of them work or I don't know how/can't find how to implement them because they're in C++ or have no explanation or context for running in C#.

        int test = SystemParametersInfo(SPI_SETBATTERYIDLETIMEOUT, 15, null, 0); //15 seconds, to test it actually working
        //test return 0

How can I, from .Net CE 3.5 in C#, change the battery timeout in Windows Mobile 6.1?

Thanks

Edit: The client requesting this application has requested this behavior specifically. They want a longer timeout during application execution and system default timeout when it's not running.

  • I found this, which could potentially work: http://stackoverflow.com/questions/2615985/keep-windows-mobile-6-phone-alive but that just keeps the phone awake indefinitely, or as long as I'm calling it. I'd prefer to let the system power management feature handle that and tell it to suspend at 15 minutes of inactivity. –  Jun 29 '11 at 22:37
  • 2
    You just can't, that function isn't available in the Mobile edition. Try to stop fretting over controlling a device that belongs to somebody else. – Hans Passant Jun 29 '11 at 23:26

3 Answers3

1

I couldn't get fluents exact method to work in a Windows Mobile 6 project in VS 2008. Firstly, the \ in the registry path were identified as control code prefixes, secondly the RegistryKey singleKey line caused an error during build. The code below did work:

var localMachine = Registry.LocalMachine;
var subKey = localMachine.OpenSubKey(@"\System\CurrentControlSet\Control\Power\Timeouts", true);
subKey.SetValue("BattSuspendTimeout", 600);

Still need to reboot to take effect though.

JamesT
  • 441
  • 4
  • 3
1

I agree with Hans that this probably the best way to annoy an end user by altering their device without asking. That said I have done something similar for a client that wanted all devices shipped with indentical setups. Rather than having a ticklist of changes to make it was faster to do in an installer.

I believe the setting you are after is held in a registry setting at

\HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Power\Timeouts

You can then alter this through the framework

RegistryKey singleKey = 
     registryKey.OpenSubKey(
     "\HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Power\Timeouts", true);

singleKey.SetValue("BattSystemIdle", 600);
singleKey.Close();

I am not 100% sure which registry key you are after but you can use the excellent Breaksoft Mobile Registry Editor to locate the exact key you need. By altering your device and keeping watch on the keys as they change you should quickly find the setting you are after.

Edit : Dead Link - Breaksoft Mobile Registry Editor

Use the alternatvie provided in the comments below

MSDN - Power Management Timeouts

fluent
  • 2,393
  • 4
  • 22
  • 32
  • That Breaksoft link is semi-broken. Breaksoft doesn't seem to be any more. Another, more capable and free registry editor for Windows CE / Mobile is CeRegEditor at http://ceregeditor.mdsoft.pl – Johann Gerell Jul 01 '11 at 08:07
  • @Johann Gerell Thanks for the heads up. I have removed the dead link. – fluent Jul 01 '11 at 09:00
  • I don't like applications that take control from me anymore than anyone else does, and would not write one willingly. However the client for this custom-application has specifically requested this behavior for their devices, as they want a longer timeout during application execution, and default timeout when it's not. –  Jul 05 '11 at 16:22
  • Setting this alone doesn't appear to enact the change and requires a restart for the changes to take affect. How can you notify the OS to reload and activate the setting change without restarting? –  Jul 05 '11 at 17:10
  • I didn't come across this as we were building an installer which performed a soft reset at the end. This question may point you in the right direction. [tell-the-os-to-reload-the-power-timeout-values-from-the-registry](http://stackoverflow.com/questions/3250929/tell-the-os-to-reload-the-power-timeout-values-from-the-registry) – fluent Jul 06 '11 at 09:26
0

For the SystemParametersInfo function, you would need to P/Invoke it using the dllimport command in C#. pinvoke.net has an example of doing this in Windows. To port it to Windows Mobile, just change the references from user32.dll to coredll.dll. http://www.pinvoke.net/default.aspx/user32.systemparametersinfo

[DllImport("coredll.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SystemParametersInfo(uint uiAction, uint uiParam, IntPtr pvParam, SPIF fWinIni);

Also consider, "What if two programs did this"?

-PaulH

PaulH
  • 7,759
  • 8
  • 66
  • 143