locked
How to correctly turn off monitor power RRS feed

  • Question

  • Hi,

    I'm already using some code to turn off the monitor power but I'm still with a little problem.

    The code works fine most of times but once a in a while, the monitor turns blank. What I mean by this is that the screen is totally black, you can't see anything, but you can see it's still lit. The LCD light is still turned on but the screen is completely black.

    I have no idea why this happens, maybe some of you know why... This will never happen if I set Windows Power settings to turn the monitor off after X minutes, but I need an application to do this so I can call it with a shortcut whenever I want.

    The code I'm using is something like this:

    1 
    2[System.Runtime.InteropServices.DllImport("user32.dll")] 
    3private static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam); 
    4 
    5const int WM_SYSCOMMAND   = 0x0112; 
    6const int SC_MONITORPOWER = 0xF170; 
    7const int HWND_BROADCAST  = 0xFFFF; 
    8 
    9SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2); 


    Any ideas?
    Tuesday, March 3, 2009 1:04 AM

All replies

  • Hi!

    Have you consider interoperating with the  Windows Power settings to use that to turn off the monitor?
    Here is something to get you started: http://www.aejw.com/default.aspx?page=dev/code/cpower

    Regards,
    Lucian Baciu, http://studentclub.ro/lucians_weblog
    Tuesday, March 3, 2009 12:04 PM
  • Didn't know I could do that...

    I'll take a deeper look at that link when I get the chance and report back.

    Thanks.
    Tuesday, March 3, 2009 1:32 PM
  • I didn't see anything helpful in that link to use windows Power Management... :(
    Thursday, March 5, 2009 12:34 AM
  • I told you that was just an example to get you started. You'll have to find the function from Power Management that shuts down the monitor. I don't know what function is that, but, if you've used it to power off the monitor yourself, then it has to exist. Look at some other samples here: http://msdn.microsoft.com/en-us/library/ms704147(VS.85).aspx 
    Lucian Baciu, http://studentclub.ro/lucians_weblog
    Thursday, March 5, 2009 7:35 AM
  • Thanks for your help, but that's too advanced for me to dig in... :(
    Monday, March 9, 2009 1:15 AM
  • Hi,

    The code works here on vista and xp system  .

    Have you tried it on other machine? Maybe the monitor does not support the functionality well.

    Best regards,
    Harry

    http://cfx.codeplex.com where you can find samples you are looking for
    Tuesday, March 10, 2009 2:01 AM
  • Yes, the code works most of times, it's not just there, it works with me too. But it's like I said, it works most of the times, not always.

    That's what I need to find out. Why it happens most of the times and others don't and how to fix it, but I have no clue how can I debug such a thing if it only happens sometimes, randomly...
    Tuesday, March 10, 2009 2:58 AM
  • Hi,

    The code works on your machine ?
    Maybe after you send the message , the mouse is moved.
    Please try use a timer to delay the message after 2 seconds.

    Best regards,
    Harry

    http://cfx.codeplex.com where you can find samples you are looking for
    Tuesday, March 10, 2009 3:07 AM
  • If that were the problem, the screen would turn on when the mouse moved... The screen is not turned on, only the backlight. Sometimes it turns off completely, others, the backlight keeps turned on, that's what I want to fix.
    Tuesday, March 10, 2009 4:07 AM
  • Hi,

    Have you tried the code on other machine? Could you please tell us the version of your system?

    Best regards,
    Harry
    http://cfx.codeplex.com where you can find samples you are looking for
    Tuesday, March 10, 2009 4:11 AM
  • No, I didn't tried it on another machine but I don't think that's relevant because I did this for me to accommodated my needs and I need it to work on my machine. Still, I don't believe this will happen on other machines, my laptop always had a few problems with Vista... And I'm running Windows Vista Business SP1.

    One thing I've noticed is this:
    Sometimes if I press the shortcut to executed the code above to turn the LCD off and the screen still has a backlit, I realized that if I close the laptop lid at least once, then open it up again and re press the shortcut buttons, the LCD will totally turn off this time. Somehow, closing the laptop lid, helps the code above to completely turn off the LCD monitor, I don't know why.

    If I could turn off the LCD monitor with the same function that is used when the LCD lid is closed, that will probably solve all my problems.
    Thursday, May 14, 2009 1:33 PM
  • Hi - I think my information can help.

    The first code examples below:


    //The code below till "suspend" our computer. 
    Application.SetSuspendState(PowerState.Suspend, true, true);
    

    The next code example follows below:
     
    //This code will "hibernate" our computer. 
    Application.SetSuspendState(PowerState.Hibernate, true, true); 
    

    The above code examples, helps us, to easy manage our computers power state.


    public int WM_SYSCOMMAND = 0x0112;
    public int SC_MONITORPOWER = 0xF170; 
    
    [DllImport("user32.dll")]
    private static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);
    
    private void button1_Click(object sender, System.EventArgs e)
    {
       SendMessage(this.Handle.ToInt32(),  WM_SYSCOMMAND, SC_MONITORPOWER, 2);
    }
    
    Try to use the code, like my above, tell me if it works. BTW, regarding the Power Manager in Windows(R) its a different function then, I think you migh need to call a C++ Win32 API, and not only the "user32.dll" but the "kernel32.dll".


    I hope the above information helped you...

    Have a nice day...

    Sincerely,
    Fisnik
    Coder24.com
    Friday, May 15, 2009 6:37 PM
  • I'm sorry but I'm not sure what you want me to do with those SetSuspendState() calls and the code below that is almost like mine...
    Friday, May 15, 2009 7:04 PM
  • Hi again:

    The "SetSuspendState() calls, was to explain how you can put your computer into sleep or hibernation mode.
    Maybe, there might be a way to suspend the monitor.

    The second code, is exactly like yours, except from "this.Handle.ToInt32()".

    I hope this helped...

    Have a nice day...

    Sincerely,
    Fisnik


    Coder24.com
    Friday, May 15, 2009 7:31 PM
  • To turn off monitor power I've used the following:

    public static class Monitor
        {
            private const int WM_SYSCOMMAND = 0x0112,
                HWND_BROADCAST = 0xFFFF,
                SC_MONITORPOWER = 0xF170,
                OFF = 2,
                STANDBY = 1,
                ON = -1;
    
            private enum BSF_FLAGS : uint
            {
                BSF_QUERY = 0x00000001,
                BSF_IGNORECURRENTTASK = 0x00000002,
                BSF_FLUSHDISK = 0x00000004,
                BSF_NOHANG = 0x00000008,
                BSF_POSTMESSAGE = 0x00000010,
                BSF_FORCEIFHUNG = 0x00000020,
                BSF_NOTIMEOUTIFNOTHUNG = 0x00000040
            }
    
            private enum BSM_RECIPIENT : uint
            {
                BSM_ALLCOMPONENTS = 0x00000000,
                BSM_VXDS = 0x00000001,
                BSM_NETDRIVER = 0x00000002,
                BSM_INSTALLABLEDRIVERS = 0x00000004,
                BSM_APPLICATIONS = 0x00000008,
                BSM_ALLDESKTOPS = 0x00000010,
            }
    
            private enum EXECUTION_STATE : uint
            {
                ES_SYSTEM_REQUIRED = 0x00000001,
                ES_DISPLAY_REQUIRED = 0x00000002,
                // Legacy flag, should not be used.
                // ES_USER_PRESENT   = 0x00000004,
                ES_CONTINUOUS = 0x80000000,
            }
    
            [DllImport("user32.dll")]
            private static extern int BroadcastSystemMessage(BSF_FLAGS flags, ref BSM_RECIPIENT recipients, int hMsg, int wParam, int lParam);
    
            [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            private static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
    
            public static void On()
            {
                BSM_RECIPIENT recipient = BSM_RECIPIENT.BSM_APPLICATIONS;
                BroadcastSystemMessage(BSF_FLAGS.BSF_FORCEIFHUNG, ref recipient, WM_SYSCOMMAND, SC_MONITORPOWER, ON);
            }
    
            public static void Off()
            {
                BSM_RECIPIENT recipient = BSM_RECIPIENT.BSM_APPLICATIONS;
                BroadcastSystemMessage(BSF_FLAGS.BSF_FORCEIFHUNG, ref recipient, WM_SYSCOMMAND, SC_MONITORPOWER, OFF);
            }
    
            public static void Standby()
            {
                BSM_RECIPIENT recipient = BSM_RECIPIENT.BSM_APPLICATIONS;
                BroadcastSystemMessage(BSF_FLAGS.BSF_FORCEIFHUNG, ref recipient, WM_SYSCOMMAND, SC_MONITORPOWER, STANDBY);
            }
    
            public static void KeepAwake()
            {
                SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_DISPLAY_REQUIRED);
            }
    
            public static void LetSleep()
            {
                SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);
            }
        }
    Does BroadcastSystemMessage work any better?
    Friday, May 15, 2009 8:06 PM
  • @Coder24.com
    It's barely the same I'm using at the moment and it doesn't work, the only different is that I'm not calling ToInt32() but I don't think that makes any difference...

    @Jonathan.Peppers
    Nope, it works the same :/
    Friday, May 15, 2009 8:23 PM