Tag Archives: screen
How to keep your screen always on
On most systems this can be configured by going to Control Panel --> Power Options --> Choose when to turn off the display
, but there are always some situations that this solution will not suffice. As an exmaple, the last time I faced this problem, Active Directory’s settings were overriding my system’s settings, which caused my display to turn off every 5 minutes regardless of how much I tried to tweak it using the Power Options.
As a workaround I decided to create a small application that would just keep the screen from turning off. I decided to share it with you in case you ever find yourself in a similar position.
You can download the program here.
If you know a bit about programming you can take a look at the source code below that will give you an idea on how this is achieved.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | using System; using System.Runtime.InteropServices; using System.Windows.Forms; namespace ScreenAlwaysOn { static class Program { [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] 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 } static NotifyIcon tIcon = new NotifyIcon(); [STAThread] static void Main() { SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_DISPLAY_REQUIRED); ContextMenu menu = new ContextMenu(); menu.MenuItems.Add("Exit", delegate { Application.Exit(); }); tIcon.Icon = Properties.Resources.monitor; tIcon.Text = "Screen Always On"; tIcon.ContextMenu = menu; tIcon.Visible = true; Application.ApplicationExit += Application_ApplicationExit; Application.Run(); } static void Application_ApplicationExit(object sender, EventArgs e) { tIcon.Dispose(); } } } |
The theory behind this solution is very simple. We are using the SetThreadExecutionState windows function to notify the system that the screen should stay on.
Flags Descriptions
ES_AWAYMODE_REQUIRED (0x00000040)
Away mode should be used only by media-recording and media-distribution applications that must perform critical background processing on desktop computers while the computer appears to be sleeping. See Remarks.
ES_CONTINUOUS(0x80000000)
Informs the system that the state being set should remain in effect until the next call that uses ES_CONTINUOUS and one of the other state flags is cleared.
ES_DISPLAY_REQUIRED(0x00000002)
Forces the display to be on by resetting the display idle timer.
ES_SYSTEM_REQUIRED(0x00000001)
Forces the system to be in the working state by resetting the system idle timer.
Posted in C#, Software Releases.
Tagged fluxbytes, fluxbytes software, screen, SetThreadExecutionState