title: Turn Display On and Kill Screensaver
body: <p>Given a Windows 8 (.1) computer with the following power options:
  <a href="https://i.stack.imgur.com/r5oJI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/r5oJI.png" alt="Power Options" /></a></p>
  <p>And screensaver configured as:
  <a href="https://i.stack.imgur.com/jWyYE.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jWyYE.png" alt="Screen Saver Settings" /></a></p>
  <p>The goal is to programmatically turn the display on and &quot;kill&quot; the screensaver (so that it will be re-activated after idle time).
  (Note, that according to the settings it's possible that only the screensaver is on, or, that the display is off completely after the screensaver was on for about one minute).</p>
  <p>What i have tried is:</p>
  <pre><code>SendMessage(HWND_Broadcast, WM_SysCommand, SC_MONITORPOWER, (LPARAM) - 1);
  </code></pre>
  <p>in combination with</p>
  <pre><code>// From Microsoft's Knowledge Base article #140723: 
  // http://support.microsoft.com/kb/140723
  // &quot;How to force a screen saver to close once started 
  // in Windows NT, Windows 2000, and Windows Server 2003&quot;
  public static void KillScreenSaver()
  {
      IntPtr hDesktop = OpenDesktop(&quot;Screen-saver&quot;, 0, false, DESKTOP_READOBJECTS | DESKTOP_WRITEOBJECTS);
      if (hDesktop != IntPtr.Zero)
      {
          if (!EnumDesktopWindows(hDesktop, KillScreenSaverFunc, IntPtr.Zero) || !CloseDesktop(hDesktop))
          {
              throw new Win32Exception(Marshal.GetLastWin32Error());
          }
      }
      else
      {
          TerminateWindow(GetForegroundWindow());
      }
  }
  
  private static bool KillScreenSaverFunc(IntPtr hWnd, IntPtr lParam)
  {
      if (IsWindowVisible(hWnd))
      {
          TerminateWindow(hWnd);
      }
  
      return true;
  }
  
  private static void TerminateWindow(IntPtr hWnd)
  {
      if (!PostMessage(hWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero))
      {
          throw new Win32Exception(Marshal.GetLastWin32Error());
      }
  }
  </code></pre>
  <p>And</p>
  <pre><code>public static void ActivateScreensaver()
  {
      SetScreenSaverActive(TRUE);
  }
  
  private static void SetScreenSaverActive(uint active)
  {
      IntPtr nullVar = IntPtr.Zero;
  
      // Ignoring error since ERROR_OPERATION_IN_PROGRESS is expected.
      // Methode is called to reset timer and to prevent possible errors as mentioned in Microsoft's Knowledge Base article #140723:
      // http://support.microsoft.com/kb/140723
      SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, active, ref nullVar, SPIF_SENDWININICHANGE);
  }
  </code></pre>
  <p>Which worked, partially. However, the display was turned off immediately again (as a matter of fact, most times one couldn't even see that the monitor was turned again apart from the power-led going &quot;steady on&quot; instead of &quot;flashing on off = power safe&quot; for a short time).</p>
  <p>So i guess i'm missing a crucial part of the picture like reliably resetting system idle timer to make sure the screen is not immediately being turned off, again.</p>
  <p>Edit: According to my investigations <code>SetThreadExecutionState(ES_DISPLAY_REQUIRED)</code> seems to do the trick in regards to resetting the idle-timer. However, i still don't know the correct sequence of calls. I'll self-answer when i figure it out...</p>