disable poweroff function
-
hi, I'm developing a mediaplayer, and I need to disable the screensaver and the poweroff functions while playing a video file. on msdn I found that I have to use the SystemParametersInfo[^] function. This code works only partially: the screensaver is disabled, but the monitor is still switched off by the system.
[DllImport("user32.dll")] private static extern bool SystemParametersInfo ( uint action, uint param, object data, uint winini ); private const int SPI_SETSCREENSAVEACTIVE = 0x0011; //17 private const int SPI_SETPOWEROFFACTIVE = 0x0056; //86 private void someMethod() { //disable screensaver. this works SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 0, null, 0); //disable poweroff. doesn't work SystemParametersInfo(SPI_SETPOWEROFFACTIVE, 0, null, 0); }
what's wrong? thank you ________ http://www.jockersoft.altervista.org -
hi, I'm developing a mediaplayer, and I need to disable the screensaver and the poweroff functions while playing a video file. on msdn I found that I have to use the SystemParametersInfo[^] function. This code works only partially: the screensaver is disabled, but the monitor is still switched off by the system.
[DllImport("user32.dll")] private static extern bool SystemParametersInfo ( uint action, uint param, object data, uint winini ); private const int SPI_SETSCREENSAVEACTIVE = 0x0011; //17 private const int SPI_SETPOWEROFFACTIVE = 0x0056; //86 private void someMethod() { //disable screensaver. this works SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 0, null, 0); //disable poweroff. doesn't work SystemParametersInfo(SPI_SETPOWEROFFACTIVE, 0, null, 0); }
what's wrong? thank you ________ http://www.jockersoft.altervista.orgI think POWEROFF refers to the system power... look for some other constant perhaps... top secret xacc-ide 0.0.1
-
I think POWEROFF refers to the system power... look for some other constant perhaps... top secret xacc-ide 0.0.1
Nope - if you read the documentation for
SystemParametersInfo
, it refers to the power phase of the screensaver.Microsoft MVP, Visual C# My Articles
-
hi, I'm developing a mediaplayer, and I need to disable the screensaver and the poweroff functions while playing a video file. on msdn I found that I have to use the SystemParametersInfo[^] function. This code works only partially: the screensaver is disabled, but the monitor is still switched off by the system.
[DllImport("user32.dll")] private static extern bool SystemParametersInfo ( uint action, uint param, object data, uint winini ); private const int SPI_SETSCREENSAVEACTIVE = 0x0011; //17 private const int SPI_SETPOWEROFFACTIVE = 0x0056; //86 private void someMethod() { //disable screensaver. this works SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 0, null, 0); //disable poweroff. doesn't work SystemParametersInfo(SPI_SETPOWEROFFACTIVE, 0, null, 0); }
what's wrong? thank you ________ http://www.jockersoft.altervista.orgThe
SPI_SETPOWEROFFACTIVE
may have more to do with the system suspension state than the monitor. The documentation is a little vague on this matter. I did try a few things. Handling theWM_POWERBROADCAST
message with thePBT_APMQUERYSUSPEND
only seems to work with the power state, so the next logical choice is something like this: P/Invoke bothGetActivePwrScheme
andSetActivePwrScheme
, as well as declare thePOWER_SCHEME
structure (you can flatten it if you like, since unmanaged structures are just blocks of contiguous memory). Information about these functions and this struct can be found in the Platform SDK at http://msdn.microsoft.com/library[^]. You can pass null (orIntPtr.Zero
) to the second parameter (declare it as anIntPtr
in order to passIntPtr.Zero
). If you do so, the signature would look like this:[DllImport("powrprof.dll")]
static extern bool SetActivePwrSchema(uint id, IntPtr globalPolicy,
ref POWER_POLICY powerPolicy);First call
GetActivePwrScheme
to get an ID for the current policy. Change thePOWER_POLICY.user.VideoTimeoutAc
(typed as it's named in the Platform SDK) to either 0 orUInt32.MaxValue
(not sure which) to set the power state of the monitor to off or a relatively indefinite time (far more than the life of a computer), whichever works (such values aren't documented). Don't forget to clean-up your code, however, like storing the previousVideoTimeoutAc
value and doing this over with the previous value. This will merge the previous state with the new state, but there's nothing I see in the documentation that dictates whether that merged state is persisted in the power scheme or if its just for the duration of the computer's up-time (still always a good idea to undo what you've done, though).Microsoft MVP, Visual C# My Articles
-
hi, I'm developing a mediaplayer, and I need to disable the screensaver and the poweroff functions while playing a video file. on msdn I found that I have to use the SystemParametersInfo[^] function. This code works only partially: the screensaver is disabled, but the monitor is still switched off by the system.
[DllImport("user32.dll")] private static extern bool SystemParametersInfo ( uint action, uint param, object data, uint winini ); private const int SPI_SETSCREENSAVEACTIVE = 0x0011; //17 private const int SPI_SETPOWEROFFACTIVE = 0x0056; //86 private void someMethod() { //disable screensaver. this works SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 0, null, 0); //disable poweroff. doesn't work SystemParametersInfo(SPI_SETPOWEROFFACTIVE, 0, null, 0); }
what's wrong? thank you ________ http://www.jockersoft.altervista.orgYou might want to check out SetThreadExecutionState[^]. Calling this function with all three flags set will let the system know that your application needs the system to be completely powered up at all times. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
You might want to check out SetThreadExecutionState[^]. Calling this function with all three flags set will let the system know that your application needs the system to be completely powered up at all times. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
thank you Heath and Dave for your hints. I followed Dave's one (much simpler) and it works. here is the code that does the job:
[DllImport("Kernel32.dll")]
private static extern uint SetThreadExecutionState
(
uint esFlags
);private const uint ES_SYSTEM_REQUIRED = 0x00000001;
private const uint ES_DISPLAY_REQUIRED = 0x00000002;
private const uint ES_CONTINUOUS = 0x80000000;private void disablePoweroff()
{
SetThreadExecutionState(ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED | ES_CONTINUOUS);
}private void enablePoweroff()
{
SetThreadExecutionState(ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED);
}thank you again, bye ________ http://www.jockersoft.altervista.org