Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. disable poweroff function

disable poweroff function

Scheduled Pinned Locked Moved C#
comquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JockerSoft
    wrote on last edited by
    #1

    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

    L H D 3 Replies Last reply
    0
    • J JockerSoft

      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

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      I think POWEROFF refers to the system power... look for some other constant perhaps... top secret xacc-ide 0.0.1

      H 1 Reply Last reply
      0
      • L leppie

        I think POWEROFF refers to the system power... look for some other constant perhaps... top secret xacc-ide 0.0.1

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        Nope - if you read the documentation for SystemParametersInfo, it refers to the power phase of the screensaver.

        Microsoft MVP, Visual C# My Articles

        1 Reply Last reply
        0
        • J JockerSoft

          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

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          The 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 the WM_POWERBROADCAST message with the PBT_APMQUERYSUSPEND only seems to work with the power state, so the next logical choice is something like this: P/Invoke both GetActivePwrScheme and SetActivePwrScheme, as well as declare the POWER_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 (or IntPtr.Zero) to the second parameter (declare it as an IntPtr in order to pass IntPtr.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 the POWER_POLICY.user.VideoTimeoutAc (typed as it's named in the Platform SDK) to either 0 or UInt32.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 previous VideoTimeoutAc 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

          1 Reply Last reply
          0
          • J JockerSoft

            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

            D Offline
            D Offline
            Dave Kreskowiak
            wrote on last edited by
            #5

            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

            J 1 Reply Last reply
            0
            • D Dave Kreskowiak

              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

              J Offline
              J Offline
              JockerSoft
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups