diff --git a/Compiled binary/NoSleep.exe b/Compiled binary/NoSleep.exe deleted file mode 100644 index b06af55..0000000 Binary files a/Compiled binary/NoSleep.exe and /dev/null differ diff --git a/Sources/NoSleep/App.config b/Sources/NoSleep/App.config index db89331..798aa6a 100644 --- a/Sources/NoSleep/App.config +++ b/Sources/NoSleep/App.config @@ -4,6 +4,9 @@
+ +
+ @@ -16,6 +19,28 @@ True + + False + + + True + + + + + NoSleep + + + NoSleep + + + 8b2caf22-dc35-4e70-88df-35933ab63f69 + + + 10000 + + + diff --git a/Sources/NoSleep/NoSleep.csproj b/Sources/NoSleep/NoSleep.csproj index 795630d..926fae6 100644 --- a/Sources/NoSleep/NoSleep.csproj +++ b/Sources/NoSleep/NoSleep.csproj @@ -42,9 +42,7 @@ - - @@ -53,6 +51,7 @@ + diff --git a/Sources/NoSleep/Program.cs b/Sources/NoSleep/Program.cs index fcf6c09..838c633 100644 --- a/Sources/NoSleep/Program.cs +++ b/Sources/NoSleep/Program.cs @@ -12,11 +12,11 @@ static class Program [STAThread] static void Main() { - using (Mutex mutex = new Mutex(false, TrayIcon.AppGuid)) + using (Mutex mutex = new Mutex(false, Properties.Settings.Default.AppMutexGuid)) { if (!mutex.WaitOne(0, false)) { - MessageBox.Show($"{TrayIcon.AppName} instance is already running.", TrayIcon.AppName, MessageBoxButtons.OK, MessageBoxIcon.Information); + MessageBox.Show($"{Properties.Settings.Default.AppName} instance is already running.", Properties.Settings.Default.AppName, MessageBoxButtons.OK, MessageBoxIcon.Information); return; } @@ -29,7 +29,8 @@ static void Main() } /// - /// If no settings were found - it's possible the program version has changed, and there are settings from a previous version. This will upgrade the settings to the current version. + /// If no settings were found - it's possible that the program version has changed and there are settings from a previous version. + /// This function will upgrade the settings to the current version - this preserves user settings. /// static void InitUpgradeSettings() { @@ -40,6 +41,5 @@ static void InitUpgradeSettings() Properties.Settings.Default.Save(); } } - } } diff --git a/Sources/NoSleep/Properties/AssemblyInfo.cs b/Sources/NoSleep/Properties/AssemblyInfo.cs index c3be050..c405e28 100644 --- a/Sources/NoSleep/Properties/AssemblyInfo.cs +++ b/Sources/NoSleep/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyVersion("1.3.0.0")] +[assembly: AssemblyFileVersion("1.3.0.0")] diff --git a/Sources/NoSleep/Properties/Settings.Designer.cs b/Sources/NoSleep/Properties/Settings.Designer.cs index de110c6..2829081 100644 --- a/Sources/NoSleep/Properties/Settings.Designer.cs +++ b/Sources/NoSleep/Properties/Settings.Designer.cs @@ -12,7 +12,7 @@ namespace NoSleep.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.7.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.12.0.0")] internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); @@ -46,5 +46,65 @@ public bool UpgradeRequired { this["UpgradeRequired"] = value; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("False")] + public bool SaveEnabledState { + get { + return ((bool)(this["SaveEnabledState"])); + } + set { + this["SaveEnabledState"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("True")] + public bool EnabledState { + get { + return ((bool)(this["EnabledState"])); + } + set { + this["EnabledState"] = value; + } + } + + [global::System.Configuration.ApplicationScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("NoSleep")] + public string AppName { + get { + return ((string)(this["AppName"])); + } + } + + [global::System.Configuration.ApplicationScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("NoSleep")] + public string AppStartupName { + get { + return ((string)(this["AppStartupName"])); + } + } + + [global::System.Configuration.ApplicationScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("8b2caf22-dc35-4e70-88df-35933ab63f69")] + public string AppMutexGuid { + get { + return ((string)(this["AppMutexGuid"])); + } + } + + [global::System.Configuration.ApplicationScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("10000")] + public int RefreshIntervalMs { + get { + return ((int)(this["RefreshIntervalMs"])); + } + } } } diff --git a/Sources/NoSleep/Properties/Settings.settings b/Sources/NoSleep/Properties/Settings.settings index 013f895..94040c7 100644 --- a/Sources/NoSleep/Properties/Settings.settings +++ b/Sources/NoSleep/Properties/Settings.settings @@ -8,5 +8,23 @@ True + + False + + + True + + + NoSleep + + + NoSleep + + + 8b2caf22-dc35-4e70-88df-35933ab63f69 + + + 10000 + \ No newline at end of file diff --git a/Sources/NoSleep/Tools.cs b/Sources/NoSleep/Tools.cs new file mode 100644 index 0000000..96425c9 --- /dev/null +++ b/Sources/NoSleep/Tools.cs @@ -0,0 +1,66 @@ +using System; +using System.IO; +using System.Windows.Forms; + +namespace NoSleep +{ + public static class Tools + { + /// Create a shortcut at given path with given link. + /// Where to target the shortcut, i.e. what to run on shortcut usage. + /// Where to create the shortcut and how to name it. + /// On IO related issues, including permissions. + public static void CreateShortcut(string targetPath, string shortcutPath) + { + var shell = new IWshRuntimeLibrary.WshShell(); + var shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath); + shortcut.TargetPath = targetPath; + shortcut.Save(); + } + /// Path to the autostart shortcut. + private static readonly string autostartPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), $"{Properties.Settings.Default.AppStartupName}.lnk"); + + /// Check application startup state + /// if application startup is enabled. + internal static bool AutostartCheck() + => File.Exists(autostartPath); + + /// Disable application startup on user login. + /// if shortcut is no longer present. + internal static bool AutostartDisable() + { + // If autostart shortcut exists - try to remove it + if (File.Exists(autostartPath)) + { + try + { + File.Delete(autostartPath); + } + catch (Exception e) + { + MessageBox.Show($"Wasn't able to remove autostart shortcut from '{autostartPath}'. Error: {e.Message}", + caption: Properties.Settings.Default.AppName, buttons: MessageBoxButtons.OK, icon: MessageBoxIcon.Error); + return false; + } + } + return true; + } + + /// Enable application startup on user login + /// if shortcut was created. + internal static bool AutostartEnable() + { + try + { + CreateShortcut(Application.ExecutablePath, autostartPath); + } + catch (Exception e) + { + MessageBox.Show($"Wasn't able to create autostart shortcut at '{autostartPath}'. Error: {e.Message}", + caption: Properties.Settings.Default.AppName, buttons: MessageBoxButtons.OK, icon: MessageBoxIcon.Error); + return false; + } + return true; + } + } +} diff --git a/Sources/NoSleep/TrayIcon.cs b/Sources/NoSleep/TrayIcon.cs index 4d815c3..0441c9b 100644 --- a/Sources/NoSleep/TrayIcon.cs +++ b/Sources/NoSleep/TrayIcon.cs @@ -1,240 +1,204 @@ -using NoSleep.Properties; -using System; -using System.IO; +using System; using System.Windows.Forms; namespace NoSleep { class TrayIcon : ApplicationContext { - internal const string AppName = "NoSleep"; - internal const string AppGuid = "8b2caf22-dc35-4e70-88df-35933ab63f69"; /// - /// Interval between timer ticks (in ms) to refresh Windows idle timers. Shouldn't be too small to avoid resources consumption. Must be less then Windows screensaver/sleep timer. - /// Default = 10 000 ms (10 seconds). + /// ExecutionMode defines how blocking is made. Mutable at runtime. /// - const int RefreshInterval = 10000; - /// - /// ExecutionMode defines how blocking is made. See details at https://msdn.microsoft.com/en-us/library/aa373208.aspx?f=255&MSPPError=-2147217396 - /// - private EXECUTION_STATE ExecutionMode = EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_AWAYMODE_REQUIRED; + private EXECUTION_STATE ExecutionMode = EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_DISPLAY_REQUIRED | + EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_AWAYMODE_REQUIRED; // PRIVATE VARIABLES - private NotifyIcon _TrayIcon; - private ToolStripMenuItem _EnabledItem; - private ToolStripMenuItem _DisplayRequired; - private readonly Timer _RefreshTimer; + private NotifyIcon _trayIcon; + private ToolStripMenuItem _menuItem_Enabled; + private ToolStripMenuItem _menuItem_DisplayRequired; + private ToolStripMenuItem _menuItem_AutoStart; + private ToolStripMenuItem _menuItem_RememberEnabledState; + private readonly Timer _refreshTimer; // CONSTRUCTOR public TrayIcon() { // Set timer to tick to refresh idle timers - _RefreshTimer = new Timer() { Interval = RefreshInterval }; - _RefreshTimer.Tick += RefreshTimer_Tick; - ArmExecutionState(); + _refreshTimer = new Timer() { Interval = Properties.Settings.Default.RefreshIntervalMs }; + _refreshTimer.Tick += RefreshTimer_Tick; // Initialize application - Application.ApplicationExit += this.OnApplicationExit; + Application.ApplicationExit += OnApplicationExit; InitializeComponent(); - _TrayIcon.Visible = true; + _trayIcon.Visible = true; + + UpdateAppEnabledState(_menuItem_Enabled.Checked); } private void InitializeComponent() { // Initialize Tray icon - _TrayIcon = new NotifyIcon + _trayIcon = new NotifyIcon { - Text = AppName, + Text = Properties.Settings.Default.AppName, Icon = Properties.Resources.TrayIcon }; - _TrayIcon.Click += TrayIcon_Click; - - // Initialize Close menu item for context menu - var _CloseMenuItem = new ToolStripMenuItem("Close"); - _CloseMenuItem.Click += CloseMenuItem_Click; - // Initialize Autostart menu item for context menu - var _AutoStartItem = new ToolStripMenuItem("Autostart at login") { Checked = LoadAutoStartPreference() }; - _AutoStartItem.Click += AutoStartItem_Click; - // Initialize EnabledItem as field, so we can reference it freely - _EnabledItem = new ToolStripMenuItem("Enabled") { Checked = true }; - _EnabledItem.Click += EnabledItem_Click; - // Initialize MonitorRequired as field, so we can reference it freely. Set it to opposite value and trigger a click once. - _DisplayRequired = new ToolStripMenuItem("Keep screen on") { Checked = !Settings.Default.DisplayRequired, ToolTipText="If display should be kept always on in addition to keeping the system on." }; - _DisplayRequired.Click += MonitorRequired_Click; - MonitorRequired_Click(null, null); - - // Initialize context menu - _TrayIcon.ContextMenuStrip = new ContextMenuStrip(); - _TrayIcon.ContextMenuStrip.Items.Add(_AutoStartItem); - _TrayIcon.ContextMenuStrip.Items.Add(_DisplayRequired); - _TrayIcon.ContextMenuStrip.Items.Add(new ToolStripSeparator()); - _TrayIcon.ContextMenuStrip.Items.Add(_EnabledItem); - _TrayIcon.ContextMenuStrip.Items.Add(_CloseMenuItem); - } + _trayIcon.Click += Click_TrayIcon; - private void MonitorRequired_Click(object sender, EventArgs e) - { - if (_DisplayRequired.Checked) + // Create tray menu items + var _MenuItem_Close = new ToolStripMenuItem("Close"); + _MenuItem_Close.Click += Click_Close; + + _menuItem_AutoStart = new ToolStripMenuItem("Autostart at login") { - _DisplayRequired.Checked = false; - // Properly disarm current state - DisarmExecutionState(); - // Update ExecutionMode - ExecutionMode &= ~EXECUTION_STATE.ES_DISPLAY_REQUIRED; - // Update settings - Settings.Default.DisplayRequired = false; - Settings.Default.Save(); - // Rearm - ArmExecutionState(); - } - else + Checked = Tools.AutostartCheck(), + ToolTipText="Should we start when you log in?" + }; + _menuItem_AutoStart.Click += Click_AutoStart; + + _menuItem_RememberEnabledState = new ToolStripMenuItem("Remember enabled state") { - _DisplayRequired.Checked = true; - // Properly disarm current state - DisarmExecutionState(); - // Update ExecutionMode - ExecutionMode |= EXECUTION_STATE.ES_DISPLAY_REQUIRED; - // Update settings - Settings.Default.DisplayRequired = true; - Settings.Default.Save(); - // Rearm - ArmExecutionState(); - } + Checked = Properties.Settings.Default.SaveEnabledState, + ToolTipText = "Should we remember the enabled state between restarts?" + }; + _menuItem_RememberEnabledState.Click += Click_SaveEnabledState; + + _menuItem_Enabled = new ToolStripMenuItem("Enabled") + { + Checked = !Properties.Settings.Default.SaveEnabledState || Properties.Settings.Default.EnabledState, + ToolTipText="Are we enabled right now?" + }; + _menuItem_Enabled.Click += Click_Enabled; + + _menuItem_DisplayRequired = new ToolStripMenuItem("Keep screen on") + { + Checked = !Properties.Settings.Default.DisplayRequired, + ToolTipText="If display should be kept always on in addition to keeping the system on." + }; + _menuItem_DisplayRequired.Click += Click_DisplayRequired; + Click_DisplayRequired(null, null); + + // Initialize context menu with created items + _trayIcon.ContextMenuStrip = new ContextMenuStrip(); + _trayIcon.ContextMenuStrip.Items.Add(_menuItem_AutoStart); + _trayIcon.ContextMenuStrip.Items.Add(_menuItem_DisplayRequired); + _trayIcon.ContextMenuStrip.Items.Add(_menuItem_RememberEnabledState); + _trayIcon.ContextMenuStrip.Items.Add(new ToolStripSeparator()); + _trayIcon.ContextMenuStrip.Items.Add(_menuItem_Enabled); + _trayIcon.ContextMenuStrip.Items.Add(_MenuItem_Close); } - private void TrayIcon_Click(object sender, EventArgs e) + // EVENT HANDLERS + /// Tray icon click (the icon ITSELF) - enable/disable. + private void Click_TrayIcon(object sender, EventArgs e) { var e2 = e as MouseEventArgs; if (e2.Button == MouseButtons.Left) - { - EnabledItem_Click(sender, e); - } + Click_Enabled(sender, e); } - private void EnabledItem_Click(object sender, EventArgs e) + /// Click on "Keep screen on" menu item - Toggle display required flag. + private void Click_DisplayRequired(object sender, EventArgs e) { - var item = _EnabledItem; - if (item.Checked) - { - item.Checked = false; - _TrayIcon.Icon = Resources.TrayIconInactive; - DisarmExecutionState(); - } - else - { - item.Checked = true; - _TrayIcon.Icon = Resources.TrayIcon; - ArmExecutionState(); - } - } + var OriginalState = _menuItem_DisplayRequired.Checked; - private void AutoStartItem_Click(object sender, EventArgs e) - { - if (!(sender is ToolStripMenuItem item)) - return; + DisarmExecutionState(); - item.Checked = item.Checked ? !RemoveFromStartup() : AddToStartup(); + _menuItem_DisplayRequired.Checked = !OriginalState; + Properties.Settings.Default.DisplayRequired = !OriginalState; + ExecutionMode = OriginalState ? ExecutionMode.DisableFlag(EXECUTION_STATE.ES_DISPLAY_REQUIRED) + : ExecutionMode.EnableFlag(EXECUTION_STATE.ES_DISPLAY_REQUIRED); + + ArmExecutionState(); + + Properties.Settings.Default.Save(); } - private void ArmExecutionState() + /// Click on "Enabled" menu item - Toggle the enabled state and update the menu item accordingly. + private void Click_Enabled(object sender, EventArgs e) { - _RefreshTimer.Start(); + _menuItem_Enabled.Checked = !_menuItem_Enabled.Checked; + UpdateAppEnabledState(_menuItem_Enabled.Checked); + SaveEnabledState(_menuItem_Enabled.Checked); } - private void DisarmExecutionState() + /// Click on "Autostart at login" menu item - Toggle the autostart state and update the menu item accordingly. + private void Click_AutoStart(object sender, EventArgs e) { - _RefreshTimer.Enabled = false; - // Clean up continuous state, if ES_CONTINUOUS was used - if (ExecutionMode.HasFlag(EXECUTION_STATE.ES_CONTINUOUS)) WinU.SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS); + // Toggle the autostart state (depending on original state) and update the menu item accordingly + // Note the reversed result for Disable. This is because function returns the opposite of what we want to set. + _menuItem_AutoStart.Checked = _menuItem_AutoStart.Checked ? !Tools.AutostartDisable() + : Tools.AutostartEnable(); } - private void OnApplicationExit(object sender, EventArgs e) + /// Click on "Remember enabled state" menu item - Toggle the state and save it and the current state to settings. + private void Click_SaveEnabledState(object sender, EventArgs e) { - _TrayIcon.Visible = false; - DisarmExecutionState(); - _RefreshTimer.Dispose(); + _menuItem_RememberEnabledState.Checked = !_menuItem_RememberEnabledState.Checked; + Properties.Settings.Default.SaveEnabledState = _menuItem_RememberEnabledState.Checked; + Properties.Settings.Default.EnabledState = _menuItem_Enabled.Checked; + Properties.Settings.Default.Save(); } - /// Close context menu item click - exit the application. - private void CloseMenuItem_Click(object sender, EventArgs e) + /// Close context menu item click - exit the application. + private void Click_Close(object sender, EventArgs e) { Application.Exit(); } + /// On application exit - hide tray icon, disarm execution state and dispose the timer. + private void OnApplicationExit(object sender, EventArgs e) + { + _trayIcon.Visible = false; + DisarmExecutionState(); + _refreshTimer.Dispose(); + } + /// Timer tick to refresh PC-required lock. private void RefreshTimer_Tick(object sender, EventArgs e) { WinU.SetThreadExecutionState(ExecutionMode); } - /// - /// Create Autostart shortcut. - /// - /// if shortcut was created. - private bool AddToStartup() + // PRIVATE METHODS + /// Arm the execution state with the __current__ ExecutionMode. + private void ArmExecutionState() { - // Get the path to the user's Startup folder - string startupFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup); - - // Copy the application executable to the Startup folder - string appExecutablePath = Application.ExecutablePath; - string appShortcutPath = Path.Combine(startupFolderPath, $"{AppName}.lnk"); - try { CreateShortcut(appExecutablePath, appShortcutPath); } - catch (Exception e) - { - MessageBox.Show($"Wasn't able to create autostart shortcut at '{appShortcutPath}'. Error: {e.Message}", AppName, MessageBoxButtons.OK, MessageBoxIcon.Error); - return false; - } - return true; + RefreshTimer_Tick(null, null); + _refreshTimer.Start(); } - /// - /// Remove Autostart shortcut. - /// - /// if shortcut is no longer present. - private bool RemoveFromStartup() + /// Disarm the execution state. If ES_CONTINUOUS was used, it will be released. + private void DisarmExecutionState() { - string startupFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup); - string appShortcutPath = Path.Combine(startupFolderPath, $"{AppName}.lnk"); - if (File.Exists(appShortcutPath)) - { - try - { - File.Delete(appShortcutPath); - } - catch (Exception e) - { - MessageBox.Show($"Wasn't able to remove autostart shortcut from '{appShortcutPath}'. Error: {e.Message}", AppName, MessageBoxButtons.OK, MessageBoxIcon.Error); - return false; - } - } - return true; + _refreshTimer.Stop(); + // If we had ES_CONTINUOUS enabled - we have to release it by calling SetThreadExecutionState with it alone (i.e. with no other flags). + if (ExecutionMode.HasFlag(EXECUTION_STATE.ES_CONTINUOUS)) + WinU.SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS); } - /// - /// Create a shortcut. Could raise on IO related issues, including permissions. - /// - /// Where to target the shortcut, i.e. what to run on shortcut usage. - /// Where to create the shortcut and how to name it. - /// - private void CreateShortcut(string targetPath, string shortcutPath) + /// Save current enabled state to settings. Actually saves only if the Save flag is enabled. + private static void SaveEnabledState(bool state) { - // Create a shortcut to the application executable - var shell = new IWshRuntimeLibrary.WshShell(); - var shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(shortcutPath); - shortcut.TargetPath = targetPath; - shortcut.Save(); + if (Properties.Settings.Default.SaveEnabledState) + { + Properties.Settings.Default.EnabledState = state; + Properties.Settings.Default.Save(); + } } - /// - /// Load AutoStart state by checking if shortcut exitst. - /// - /// if autostart is enabled. - private bool LoadAutoStartPreference() + /// Update the application state to reflect enabled state - tray icon and execution state timer. + private void UpdateAppEnabledState(bool state) { - string startupFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup); - string appShortcutPath = Path.Combine(startupFolderPath, $"{AppName}.lnk"); - return File.Exists(appShortcutPath); + if (state) + { + _trayIcon.Icon = Properties.Resources.TrayIcon; + ArmExecutionState(); + } + else + { + _trayIcon.Icon = Properties.Resources.TrayIconInactive; + DisarmExecutionState(); + } } } } \ No newline at end of file diff --git a/Sources/NoSleep/WinU.cs b/Sources/NoSleep/WinU.cs index 42dae70..b8e18bd 100644 --- a/Sources/NoSleep/WinU.cs +++ b/Sources/NoSleep/WinU.cs @@ -3,20 +3,12 @@ namespace NoSleep { - /* - * Value Meaning - -ES_SYSTEM_REQUIRED -0x00000001 - - Forces the system to be in the working state by resetting the system idle timer. - -ES_USER_PRESENT -0x00000004 - - This value is not supported. If ES_USER_PRESENT is combined with other esFlags values, the call will fail and none of the specified states will be set. - */ - [FlagsAttribute] + /// + /// Windows Execution State ENUM with available and not deprecated flags. + /// See SetThreadExecutionState. + /// See article for details. + /// + [Flags] internal enum EXECUTION_STATE : uint { /// No flags. Should NEVER be used. Either use ES_CONTINUOUS with no other flags (if previously used) or nothing. @@ -33,9 +25,25 @@ internal enum EXECUTION_STATE : uint ES_CONTINUOUS = 0x80000000 } + /// + /// Extension methods for enum. + /// + internal static class ExecutionStateEnumExtensions + { + internal static EXECUTION_STATE EnableFlag(this EXECUTION_STATE value, EXECUTION_STATE flag) => value | flag; + internal static EXECUTION_STATE DisableFlag(this EXECUTION_STATE value, EXECUTION_STATE flag) => value & ~flag; + internal static EXECUTION_STATE ToggleFlag(this EXECUTION_STATE value, EXECUTION_STATE flag) => value ^ flag; + } + + /// + /// Win32 API wrapper. + /// internal static class WinU { - // Import SetThreadExecutionState Win32 API and necessary flags + /// + /// Import SetThreadExecutionState from Win32 API. + /// See SetThreadExecutionState. + /// [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] static internal extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags); } diff --git a/readme.html b/readme.html deleted file mode 100644 index f28726d..0000000 --- a/readme.html +++ /dev/null @@ -1,114 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - -
-

NoSleep windows utility

-

13 July 2016

-

NoSleep utility prevents Windows OS from going into ScreenSaver / Sleep / ScreenLock modes. It’s meant for those cases where user cannot change settings due to corporate-enforced policy.

-
-

Usage

-

Download NoSleep.exe from “Compiled binary” folder. Then just run and forget. It will sit in tray and work. If you wish to close the program - right-click tray icon and select “Close”.

-
-
-

Requirements

-

.NET 4.0 (client profile)

-
-
-

How it works

-

NoSleep works through calling to SetThreadExecutionState resetting display/idle Windows timers every 10 seconds. Should use no CPU and around 6-7 MBs of RAM. Icons are taking up 166 KB in compiled binary, the rest is the code (~10 KB).

-
-
-

Icon

-

Icon was taken from http://www.iconarchive.com/show/food-icons-by-martin-berube/coffee-icon.html. Icon is disributed as freeware.

-

Artist: Martin Berube (thanks, Martin!)

-
-
-

License

-

Completely free, use as you will. No liability.

-
-
- - -
- - - - - - - -