title: Prevent screen from sleeping with C#
body: <p>I have created a small C# console app to move the pointer around the screen, in the hope that this would prevent the screen from sleeping / locking after a few minutes. Unfortunately the screen still goes to sleep after a few minutes.</p>
  
  <p>Does anyone know if it's actually possible to write something in C# which will act like user input (either mouse or keyboard), and prevent the screen from sleeping / locking automatically?</p>
  
  <p>Here is what I have, which I thought might do the trick.</p>
  
  <pre><code>class Program
  {
      [DllImport("user32.dll")]
      static extern bool SetCursorPos(int X, int Y);
  
      static Random rnd = new Random();
  
      static void Main(string[] args)
      {
  
          Rectangle screenRes = Screen.PrimaryScreen.Bounds;
          int widtMax = screenRes.Width;
          int heighMax = screenRes.Height;
  
          int w;
          int h;
  
          do
          {
              while (!Console.KeyAvailable)
              {
                  w = rnd.Next(1, widtMax);
                  h = rnd.Next(1, heighMax);
  
                  SetCursorPos(w, h);
                  System.Threading.Thread.Sleep(1000);
              }
          } while (Console.ReadKey(true).Key != ConsoleKey.Escape);
      }
  }
  </code></pre>