Compile this in Visual Studio or C# Express and run it from a command prompt (or double click it). Requires .NET 4.0 or above. It does everything you are looking for.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Threading;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ImWorkin
{
class Program
{
[FlagsAttribute]
public enum EXECUTION_STATE : uint
{
ES_SYSTEM_REQUIRED = 0x00000001,
ES_DISPLAY_REQUIRED = 0x00000002,
ES_CONTINUOUS = 0x80000000
}
public SYSTEMTIMEOUTS TimeOuts
{
get { return sysTimeouts; }
}
public struct SYSTEMTIMEOUTS
{
public int BATTERYIDLETIMEOUT;
public int EXTERNALIDLETIMEOUT;
public int WAKEUPIDLETIMEOUT;
}
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE flags);
[DllImport("user32.dll", SetLastError = true, EntryPoint ="SystemParametersInfo")]
internal static extern int SystemParametersInfo(int uiAction, int uiParam, ref int pvParam, int fWinIni);
private static System.Threading.Timer preventSleepTimer = null;
public const int SPI_GETBATTERYIDLETIMEOUT = 252;
public const int SPI_GETEXTERNALIDLETIMEOUT = 254;
public const int SPI_GETWAKEUPIDLETIMEOUT = 256;
public static int Counter = 0;
public static int timeOutinMS = 0;
public static int batteryIdleTimer;
public static int externalIdleTimer;
public static int wakeupIdleTimer;
public static SYSTEMTIMEOUTS sysTimeouts;
static void Main(string[] args)
{
Console.WriteLine("You are about to be workin!! Just a moment...I need to calculate a few values.");
string dots = string.Empty;
for (int i =2; i < 60; i++)
{
dots = "";
for (int ii = 0; ii < i; ii++)
{
dots = dots + ".";
}
Thread.Sleep(100);
Console.Clear();
Console.WriteLine("You are about to be workin!! Just a moment...I need to calculate a few values.");
Console.WriteLine(dots);
}
GetSystemTimeOuts();
if (timeOutinMS < sysTimeouts.BATTERYIDLETIMEOUT)
timeOutinMS = sysTimeouts.BATTERYIDLETIMEOUT;
if (timeOutinMS < sysTimeouts.EXTERNALIDLETIMEOUT)
timeOutinMS = sysTimeouts.EXTERNALIDLETIMEOUT;
if (timeOutinMS < sysTimeouts.WAKEUPIDLETIMEOUT)
timeOutinMS = sysTimeouts.WAKEUPIDLETIMEOUT;
if (timeOutinMS == 0)
timeOutinMS = 30;
DisableDeviceSleep();
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("OK. I have calculated your computers timeout periods and set the ");
Console.WriteLine("necessary hooks. Your computer will not shut off the monitor, will");
Console.WriteLine("show active in any chat programs,the screensaver is disabled and ");
Console.WriteLine("the computer will not lock! Anyone looking at you eaither locally ");
Console.WriteLine("or remotely will think you are hard at work.");
Console.WriteLine("");
Console.WriteLine("Now go do something fun...I got your back ;)");
Console.WriteLine("Oh yeah....if you close this window OR press `q' in this ");
Console.WriteLine("window you are going to have to actually work.");
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("This text will disappear in a 30 seconds. Just in case someone comes ");
Console.WriteLine("by and reads your screen!");
Console.WriteLine("");
Console.WriteLine("");
Console.WriteLine("Need custom coding? Kenneth.gore@gmail.com");
while (Console.KeyAvailable == false)
{
Thread.Sleep(250);
ConsoleKeyInfo cki = Console.ReadKey(true);
if (cki.KeyChar == 'q')
break;
}
}
public static void DisableDeviceSleep()
{
SetThreadExecutionState(EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS);
preventSleepTimer = new System.Threading.Timer(new TimerCallback(PokeDeviceToKeepAwake), null, 0, timeOutinMS * 1000);
}
public static void EnableDeviceSleep()
{
preventSleepTimer.Dispose();
preventSleepTimer = null;
}
private static void PokeDeviceToKeepAwake(object extra)
{
Counter++;
try
{
SetThreadExecutionState(EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS);
IntPtr Handle = FindWindow("SysListView32", "FolderView");
if (Handle == IntPtr.Zero)
{
SetForegroundWindow(Handle);
SendKeys.SendWait("%1");
}
if (Counter > 1)
Console.Clear();
}
catch
{
}
}
public static void GetSystemTimeOuts()
{
sysTimeouts.BATTERYIDLETIMEOUT = -2;
sysTimeouts.EXTERNALIDLETIMEOUT = -2;
sysTimeouts.WAKEUPIDLETIMEOUT = -2;
if (SystemParametersInfo(SPI_GETBATTERYIDLETIMEOUT, 0, ref batteryIdleTimer, 0) == 1)
sysTimeouts.BATTERYIDLETIMEOUT = batteryIdleTimer;
else
sysTimeouts.BATTERYIDLETIMEOUT = -1;
if (SystemParametersInfo(SPI_GETEXTERNALIDLETIMEOUT, 0, ref externalIdleTimer, 0) == 1)
sysTimeouts.EXTERNALIDLETIMEOUT = externalIdleTimer;
else
sysTimeouts.EXTERNALIDLETIMEOUT = -1;
if (SystemParametersInfo(SPI_GETWAKEUPIDLETIMEOUT, 0, ref wakeupIdleTimer, 0) == 1)
sysTimeouts.WAKEUPIDLETIMEOUT = wakeupIdleTimer;
else
sysTimeouts.WAKEUPIDLETIMEOUT = -1;
}
}
}
1The grace period doesn't work for me, while the script does. I assume that my corporate policy also disables the grace period. – Dane Jacob Hampton – 2018-04-20T08:41:28.650
1Late to the game here, but wanted to add my solution. Since I don't have admin rights to the PC I use at work, and none of the scripts or programs in this thread worked for me (either they weren't allowed to be installed, or just didn't work), I purchased a "USB Mouse Jiggle" hardware from Amazon. Works like a dream, only $15. – Jeff Wright – 2018-04-30T11:01:54.497
15 minutes for screen locking is absolutely across-the-board standard. It's by no means "ridiculously short" other than OP's ancillary experience and opinion. BTW, this is usually mandated by corporate policy, which IT does not make (we just enforce it or suffer the consequences). – SamAndrew81 – 2019-10-28T18:26:11.100
My previous company has a 2-minute screen lock policy. That's not ridiculous but disaster and torturous and sometimes depressing. – Lame User – 2019-12-27T08:07:40.787
2Just curious what "ridiculously short" is? – Carl – 2011-08-30T15:45:32.970
There are workarounds like key pressers and mouse movers that will prevent the screen from locking. But are you sure you want to do this? Circumventing IT security is more than likely against company policy, and could be a fireable offense. – Keltari – 2011-08-30T16:14:38.273
715 minutes. OK, it's not that short ... unless you are at home, not in an open space, and also often working on a second computer. Like I am, so the screen lock is obnoxious. – Gabriel R. – 2011-08-31T10:32:52.927
One semi workaround is not to block the screensaver, but to set the grace period to a few hours. (That is the time between the starting of the screensaver and the time you need to enter a password. Usually this is set to 5 seconds. So a quick shake with the mouse when the screensaver just kicks in disables it. However with a higher value you can have hours without a password. – Hennes – 2013-12-24T20:14:09.017