5

I want to stop my monitor to stop sleep (which is driven by company policies). Below is the code I am using for that

while (true)
{
    this.Cursor = new Cursor(Cursor.Current.Handle);
    Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50);
    Cursor.Clip = new Rectangle(this.Location, this.Size);

    System.Threading.Thread.Sleep(2000);

    this.Cursor = new Cursor(Cursor.Current.Handle);
    Cursor.Position = new Point(Cursor.Position.X + 50, Cursor.Position.Y + 50);
    Cursor.Clip = new Rectangle(this.Location, this.Size);
}

I can see mouse moving without While loop. But with while it moves mouse only once and then it restrict the mouse to move to right side.

Is there any better way to do it?

Humayun Shabbir
  • 2,961
  • 4
  • 20
  • 33
Zerotoinfinity
  • 6,290
  • 32
  • 130
  • 206
  • 1
    http://www.perlmonks.org/?node=xy+problem – EZI Jul 21 '14 at 16:04
  • add a `Thread.Sleep(2000);` in the end, then you will see the move. but I don't know if that will prevent monitor from sleeping.. – Bolu Jul 21 '14 at 16:05
  • There is a reason monitors have been set to sleep by your company policy, breaking those policies sounds like a baaaaad idea. – DavidG Jul 21 '14 at 16:06
  • @Bolu Yes, without while loop it is moving to XY coordinate and coming back to the original position but not with the while loop. – Zerotoinfinity Jul 21 '14 at 16:13
  • I was not asking.... I said you need an extra `Thread.Sleep(2000)` in the end of your while loop. as your current code will move the mouse to right and immediately to left again (by the loop, so you won't notice the move).... – Bolu Jul 21 '14 at 16:26
  • you want to disable sleeping mode of your computer ? – Xaruth Jul 21 '14 at 16:27
  • How do you run this program on your computer – Giriraj Feb 04 '20 at 09:57
  • @Giriraj I had created a windows form project in C# – Zerotoinfinity Feb 04 '20 at 16:28

2 Answers2

10

If you want to keep your computer awake, dont move the mouse, just tell your program that computer must keep awake. Moving the mouse is a very bad practice.

public class PowerHelper
{
    public static void ForceSystemAwake()
    {
        NativeMethods.SetThreadExecutionState(NativeMethods.EXECUTION_STATE.ES_CONTINUOUS |
                                              NativeMethods.EXECUTION_STATE.ES_DISPLAY_REQUIRED |
                                              NativeMethods.EXECUTION_STATE.ES_SYSTEM_REQUIRED |
                                              NativeMethods.EXECUTION_STATE.ES_AWAYMODE_REQUIRED);
    }

    public static void ResetSystemDefault()
    {
        NativeMethods.SetThreadExecutionState(NativeMethods.EXECUTION_STATE.ES_CONTINUOUS);
    }
}

internal static partial class NativeMethods
{
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);

    [FlagsAttribute]
    public enum EXECUTION_STATE : uint
    {
        ES_AWAYMODE_REQUIRED = 0x00000040,
        ES_CONTINUOUS = 0x80000000,
        ES_DISPLAY_REQUIRED = 0x00000002,
        ES_SYSTEM_REQUIRED = 0x00000001

        // Legacy flag, should not be used.
        // ES_USER_PRESENT = 0x00000004
    }
}

and then, just call ForceSystemAwake() when you want to keep awake your computer, then call ResetSystemDefault() when you have finish

Xaruth
  • 4,034
  • 3
  • 19
  • 26
2

This method moves the mouse by 1 pixel every 4 minutes and it will not let your monitor to sleep.

using System;
using System.Drawing;
using System.Windows.Forms;

static class Program
{
    static void Main()
    {
        Timer timer = new Timer();
        // timer.Interval = 4 minutes
        timer.Interval = (int)(TimeSpan.TicksPerMinute * 4 /TimeSpan.TicksPerMillisecond);
        timer.Tick += (sender, args) => { Cursor.Position = new Point(Cursor.Position.X + 1, Cursor.Position.Y + 1); };
        timer.Start();
        Application.Run();
    }
}
Vibhore Jain
  • 174
  • 7
  • System.Timers.Timer does not have a Tick events. – Liakat Hossain May 11 '18 at 11:36
  • 1
    @Liakat they aren't using System.Timers.Timer, they are using System.Windows.Forms.Timer which does have a tick event. – Andy Jan 23 '20 at 15:49
  • If you are working in WPF use the dispatch timer. https://stackoverflow.com/questions/11559999/how-do-i-create-a-timer-in-wpf – Jeff Jun 27 '22 at 20:33