How to disable screen saver/auto power off in Compact Framework (C#)

L

LEM

Hi all,

Anybody knows a way of doing that? I have tried several things but it
doesn't work.

This is what I have tried:

[System.Runtime.InteropServices.DllImport("coredll.dll")]
private static extern int SystemParametersInfo(
int uiAction, int uiParam, string pvParam, int fWinIni);


private const int SPI_SETLOWPOWERTIMEOUT = 81;
private const int SPI_SETPOWEROFFTIMEOUT = 82;
private const int SPI_SETSCREENSAVETIMEOUT = 15;
private const int SPI_SETSCREENSAVEACTIVE = 17;

public static void DisableScreenSaver()
{
SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, 0, null, 0);
SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, null, 0);
SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, null, 0);

SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 0, null, 0);
}


I hope somebody can help me with this, because I don't know what else to
try.

Thanks
 
G

Guest

IIRC it's a registry entry change followed by a broadcast of
WM_SETTINGCHANGE. The way to know exactly is to either look at the control
panel source code in Platform Builder, or to do a registry diff and use Spy
to see what messages get sent when you make the change in the control panel.
 
L

LEM

which would be a very poor design

Ok, in that case what do you suggest for this? My application needs to
read information from an internal GPS while is running. If you power off
the device, the GPS will turn off automatically, so my application will
stop working. That's why I need to disable it while my program is running.





If you just want to delay the automatic turn-off while you do something,
rather than preventing it forever, which would be a very poor design, you
can call SystemIdleTimerReset(). There are tons of mentions of this in the
old messages in this group, which you can search from here:

http://groups.google.com/group/micr...ork/topics?hl=en&lr=lang_en&ie=UTF-8&oe=UTF-8

Paul T.

LEM said:
Hi all,

Anybody knows a way of doing that? I have tried several things but it
doesn't work.

This is what I have tried:

[System.Runtime.InteropServices.DllImport("coredll.dll")]
private static extern int SystemParametersInfo(
int uiAction, int uiParam, string pvParam, int fWinIni);


private const int SPI_SETLOWPOWERTIMEOUT = 81;
private const int SPI_SETPOWEROFFTIMEOUT = 82;
private const int SPI_SETSCREENSAVETIMEOUT = 15;
private const int SPI_SETSCREENSAVEACTIVE = 17;

public static void DisableScreenSaver()
{
SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, 0, null, 0);
SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, null, 0);
SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, null, 0);

SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 0, null, 0);
}


I hope somebody can help me with this, because I don't know what else to
try.

Thanks
 
P

Paul G. Tobey [eMVP]

As indicated, when you need the device to stay awake while operation A is
running, call SystemIdleTimerReset. Don't change the on/off times just to
keep the device awake while operation A is running...

Paul T.

LEM said:
which would be a very poor design

Ok, in that case what do you suggest for this? My application needs to
read information from an internal GPS while is running. If you power off
the device, the GPS will turn off automatically, so my application will
stop working. That's why I need to disable it while my program is running.





If you just want to delay the automatic turn-off while you do something,
rather than preventing it forever, which would be a very poor design, you
can call SystemIdleTimerReset(). There are tons of mentions of this in
the old messages in this group, which you can search from here:

http://groups.google.com/group/micr...ork/topics?hl=en&lr=lang_en&ie=UTF-8&oe=UTF-8

Paul T.

LEM said:
Hi all,

Anybody knows a way of doing that? I have tried several things but it
doesn't work.

This is what I have tried:

[System.Runtime.InteropServices.DllImport("coredll.dll")]
private static extern int SystemParametersInfo(
int uiAction, int uiParam, string pvParam, int fWinIni);


private const int SPI_SETLOWPOWERTIMEOUT = 81;
private const int SPI_SETPOWEROFFTIMEOUT = 82;
private const int SPI_SETSCREENSAVETIMEOUT = 15;
private const int SPI_SETSCREENSAVEACTIVE = 17;

public static void DisableScreenSaver()
{
SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, 0, null, 0);
SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, null, 0);
SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, null, 0);

SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 0, null, 0);
}


I hope somebody can help me with this, because I don't know what else to
try.

Thanks
 
L

LEM

Thanks Paul.

Before you answered the first time I found a way modifying the registry
(BattSuspendTimeout) and changing it back when the application was closed.
However I think it's cleaner to use SystemIdleTimerReset.
Don't you think so?

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top