locked
Window 10 - c# code to wake and turn on monitor RRS feed

  • Question

  • Hello,

    I am able to wake:

    CreateWaitableTimer

    On windows 10, no matter what I try, I cannot seem to get the monitor to turn on along with the system wake.  I have tryied using calls to move the mouse but to no avail.   This is specific to a windows 10 install.

    Any help?  Thanks!


    TFM

    Monday, February 1, 2016 9:55 PM

Answers

  • Hi Kristin,

    Thanks for the link.  I tried the sendmessage option previously and it did not seem to wake the screen after sleep (I may have done something wrong).   I just finally found the following mouse_event to work:

    [DllImport("user32.dll")]
    static extern void mouse_event(Int32 dwFlags, Int32 dx, Int32 dy, Int32 dwData, UIntPtr dwExtraInfo);
    private const int MOUSEEVENTF_MOVE = 0x0001;
    
    mouse_event(MOUSEEVENTF_MOVE, 0, 1, 0, UIntPtr.Zero);


    TFM

    • Marked as answer by tmuyiuy Tuesday, February 2, 2016 10:18 PM
    Tuesday, February 2, 2016 10:18 PM

All replies

  • Hi TFM,

    This worked for me on windows 8. I followed this codeproject article and its the same thing as this as http://www.codeproject.com/Articles/11099/Turn-on-off-monitor    

    Does this helps you on win10?

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Andy ONeill Tuesday, February 2, 2016 6:11 AM
    Tuesday, February 2, 2016 3:31 AM
  • Hi Kristin,

    Thanks for the link.  I tried the sendmessage option previously and it did not seem to wake the screen after sleep (I may have done something wrong).   I just finally found the following mouse_event to work:

    [DllImport("user32.dll")]
    static extern void mouse_event(Int32 dwFlags, Int32 dx, Int32 dy, Int32 dwData, UIntPtr dwExtraInfo);
    private const int MOUSEEVENTF_MOVE = 0x0001;
    
    mouse_event(MOUSEEVENTF_MOVE, 0, 1, 0, UIntPtr.Zero);


    TFM

    • Marked as answer by tmuyiuy Tuesday, February 2, 2016 10:18 PM
    Tuesday, February 2, 2016 10:18 PM
  • Hi TFM

    Is this working in windows 10 for you? mine is not working...


    sandeep chauhan

    Thursday, May 17, 2018 6:48 AM
  • Yes, all my computers are win 10 and when the computer wakes from sleep I am doing a:

    mouse_event(MOUSEEVENTF_MOVE, 0, 1, 0, UIntPtr.Zero);
    and the display wakes.


    TFM

    Thursday, May 17, 2018 3:49 PM
  •  not working for me :(

    Any specific setting or anything which I am missing.

    Here is my code on Page Loaded event. pls let me know where i am doing wrong.

     private const int HWND_BROADCAST = 0xffff;
            private const int WM_SYSCOMMAND = 0x0112;
            private const int SC_MONITORPOWER = 0xF170;
            private const int SC_MONITORPOWER_OFF = 2;
      
            private const int MOUSEEVENTF_MOVE = 0x0001;

            [DllImport("user32.dll")]
            public static extern IntPtr SendMessageW(int hWnd, uint Msg, int wParam, int lParam);

            [DllImport("user32.dll")]
            static extern void mouse_event(Int32 dwFlags, Int32 dx, Int32 dy, Int32 dwData, UIntPtr dwExtraInfo);

            public MonitorON()
            {
                this.InitializeComponent();

                this.Loaded += MonitorON_Loaded;
            }

            private void MonitorON_Loaded(object sender, RoutedEventArgs e)
            {
                 
                var t = SendMessageW(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, SC_MONITORPOWER_OFF);

                System.Threading.Tasks.Task.Delay(2000);
                Wake();      
            }
            private void Wake()
            {
                mouse_event(MOUSEEVENTF_MOVE, 0, 1, 0, UIntPtr.Zero);
            }


    sandeep chauhan

    Friday, May 18, 2018 6:05 AM
  • I don't have anything in my code to do with the monitor power.   When my computers wake, the display wakes.   When my code continues on wake from sleep (SetWaitableTimer), I immediately do the mouse_event where it looks like you are waiting 2 seconds?

    TFM

    Wednesday, May 23, 2018 5:22 PM
  • The question here specifies C#, but just as an aside, for those looking for a way to do this in .bat files, scripts, or Task Scheduler actions, this line in a .bat script will move the mouse 40 pixels right and consequently wake the display (using the same method as tmuyiuy's answer):

    powershell (Add-Type '[DllImport(\"user32.dll\")]^public static extern void mouse_event(uint dwFlags, int dx, int dy, uint dwData, int dwExtraInfo);' -Name user32 -PassThru)::mouse_event(1,40,0,0,0)

    (quite inefficient, since it uses just-in-time compilation, but it works!)


    • Edited by dean999 Sunday, January 20, 2019 10:06 AM
    Sunday, January 20, 2019 10:05 AM