<p>OK. This is a bit of a vanity app, but I had a situation today at work where I was in a training class and the machine was set to lock every 10 minutes.  Well, if the trainers got excited about talking - as opposed to changing slides - the machine would lock up.</p>

<p>I'd like to write a teeny app that has nothing but a taskbar icon that does nothing but move the mouse by 1 pixel every 4 minutes.  </p>

<p>I can do that in 3 ways with Delphi (my strong language) but I'm moving to C# for work and I'd like to know the path of least resistance there.</p>

[{'answer_id': 1843, 'author': 'lubos hasko', 'author_id': 275, 'author_profile': 'https://Stackoverflow.com/users/275', 'pm_score': 6, 'selected': True, 'text': '<p><strong>for C# 3.5</strong></p>\n\n<p>without notifyicon therefore you will need to terminate this application in task manager manually</p>\n\n<pre><code>using System;\nusing System.Drawing;\nusing System.Windows.Forms;\n\nstatic class Program\n{\n    static void Main()\n    {\n        Timer timer = new Timer();\n        // timer.Interval = 4 minutes\n        timer.Interval = (int)(TimeSpan.TicksPerMinute * 4 / TimeSpan.TicksPerMillisecond);\n        timer.Tick += (sender, args) =&gt; { Cursor.Position = new Point(Cursor.Position.X + 1, Cursor.Position.Y + 1); };\n        timer.Start();\n        Application.Run();\n    }\n}\n</code></pre>\n'}, {'answer_id': 1849, 'author': 'DylanJ', 'author_id': 87, 'author_profile': 'https://Stackoverflow.com/users/87', 'pm_score': 2, 'selected': False, 'text': '<p>Something like this should work (though, you <strong>will</strong> want to change the interval).</p>\n\n<pre><code>public Form1()\n{\n    InitializeComponent();\n    Timer Every4Minutes = new Timer();\n    Every4Minutes.Interval = 10;\n    Every4Minutes.Tick += new EventHandler(MoveNow);\n    Every4Minutes.Start();\n}\n\nvoid MoveNow(object sender, EventArgs e)\n{\n    Cursor.Position = new Point(Cursor.Position.X - 1, Cursor.Position.Y - 1);\n}\n</code></pre>\n'}, {'answer_id': 13910, 'author': 'Zooba', 'author_id': 891, 'author_profile': 'https://Stackoverflow.com/users/891', 'pm_score': 4, 'selected': False, 'text': '<p>The "correct" way to do this is to respond to the WM_SYSCOMMAND message. In C# this looks something like this:</p>\n\n<pre><code>protected override void WndProc(ref Message m)\n{\n    // Abort screensaver and monitor power-down\n    const int WM_SYSCOMMAND = 0x0112;\n    const int SC_MONITOR_POWER = 0xF170;\n    const int SC_SCREENSAVE = 0xF140;\n    int WParam = (m.WParam.ToInt32() &amp; 0xFFF0);\n\n    if (m.Msg == WM_SYSCOMMAND &amp;&amp;\n        (WParam == SC_MONITOR_POWER || WParam == SC_SCREENSAVE)) return;\n\n    base.WndProc(ref m);\n}\n</code></pre>\n\n<p>According to <a href="http://msdn.microsoft.com/en-us/library/ms646360(VS.85).aspx" rel="noreferrer">MSDN</a>, if the screensaver password is enabled by policy on Vista or above, this won\'t work. Presumably programmatically moving the mouse is also ignored, though I have not tested this.</p>\n'}, {'answer_id': 8451344, 'author': 'Heywoody', 'author_id': 1090471, 'author_profile': 'https://Stackoverflow.com/users/1090471', 'pm_score': 3, 'selected': False, 'text': '<p>When I work from home, I do this by tying the mouse cord to a desktop fan which oscillates left to right. It keeps the mouse moving and keeps the workstation from going to sleep.</p>\n'}, {'answer_id': 65312692, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>(Windows 10 / .Net 5 / C# 9.0)</p>\n<p>Instead of faking activity, you could</p>\n<blockquote>\n<p>inform the system that it is in use, thereby preventing the system\nfrom entering sleep or turning off the display while the application\nis running</p>\n</blockquote>\n<p>using <a href="https://learn.microsoft.com/fr-fr/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate" rel="nofollow noreferrer">SetThreadExecutionState</a>, as described on <a href="https://www.pinvoke.net/default.aspx/kernel32.setthreadexecutionstate" rel="nofollow noreferrer">PInvoke.net</a> :</p>\n<pre><code>using System;\nusing System.Runtime.InteropServices;\nusing System.Threading;\n\nnamespace VanityApp\n{\n    internal static class Program\n    {\n        [DllImport(&quot;kernel32.dll&quot;, CharSet = CharSet.Auto, SetLastError = true)]\n        private static extern ExecutionState SetThreadExecutionState(ExecutionState esFlags);\n\n        [Flags]\n        private enum ExecutionState : uint\n        {\n            ES_AWAYMODE_REQUIRED = 0x00000040,\n            ES_CONTINUOUS = 0x80000000,\n            ES_DISPLAY_REQUIRED = 0x00000002,\n            ES_SYSTEM_REQUIRED = 0x00000001\n        }\n\n        private static void Main()\n        {\n            using AutoResetEvent autoResetEvent = new AutoResetEvent(false);\n            using Timer timer = new Timer(state =&gt; SetThreadExecutionState(ExecutionState.ES_AWAYMODE_REQUIRED | ExecutionState.ES_CONTINUOUS | ExecutionState.ES_DISPLAY_REQUIRED | ExecutionState.ES_SYSTEM_REQUIRED), autoResetEvent, 0, -1);\n            autoResetEvent.WaitOne();\n        }\n    }\n}\n</code></pre>\n<p>The Timer is a <a href="https://learn.microsoft.com/en-us/dotnet/api/system.threading.timer" rel="nofollow noreferrer">System.Threading.Timer</a>, with its handy constructor, and it uses <a href="https://learn.microsoft.com/en-us/dotnet/api/system.threading.waithandle.waitone" rel="nofollow noreferrer">AutoResetEvent.WaitOne()</a> to avoid exiting immediately.</p>\n'}, {'answer_id': 65621355, 'author': 'Mike O.', 'author_id': 8738017, 'author_profile': 'https://Stackoverflow.com/users/8738017', 'pm_score': 1, 'selected': False, 'text': "<p>Raf provided a graceful answer to the problem for Win10 world, but unfortunately, his autoResetEvent.WaitOne() instruction blocks the thread, and therefore it must be in a separate thread of its own.</p>\n<p>What worked for me can actually run in the main thread, the code doesn't have to be placed in the Main() method, and you can actually have a button to enable this functionality and one to disable it.</p>\n<p>First, you certainly need to define the execution state flags:</p>\n<pre><code>[Flags]\nprivate enum ExecutionState : uint // options to control monitor behavior\n{\n  ES_AWAYMODE_REQUIRED = 0x00000040, // prevent idle-to-sleep\n  ES_CONTINUOUS = 0x80000000, // allow monitor power down\n  ES_DISPLAY_REQUIRED = 0x00000002, // prevent monitor power down\n  ES_SYSTEM_REQUIRED = 0x00000001 // keep system awake\n}\n</code></pre>\n<p>Now, whenever you want to keep your system awake and block your monitor from turning off or idling to sleep, all you need to do, is execute a single command:</p>\n<pre><code>SetThreadExecutionState(ExecutionState.ES_AWAYMODE_REQUIRED | ExecutionState.ES_CONTINUOUS | ExecutionState.ES_DISPLAY_REQUIRED | ExecutionState.ES_SYSTEM_REQUIRED);\n</code></pre>\n<p>Then, if you want to undo this action and return your system back to its original execution state, just issue the following command:</p>\n<pre><code>SetThreadExecutionState(ExecutionState.ES_CONTINUOUS);\n</code></pre>\n<p>Keep in mind, each command will return the previous execution state, which means, when you first alter this state, you can cache the returned value locally and use it if/when you want to restore the previous state.</p>\n"}]
['https://Stackoverflow.com/questions/1836', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/172/']