开发者

How to change the battery idle timeout programmatically in .Net on Windows Mobile 6

开发者 https://www.devze.com 2023-03-16 13:44 出处:网络
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\

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 t开发者_Go百科o 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 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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消