Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Windows workstation (desktop) from locking while running a WPF program

Issue:
I have a WPF fullscreen application, which acts as a dashboard. The computer is in domain and domain policies enforce the computer to be locked in 10 minutes after the last user activity. I want to prevent the workstation (or desktop) from locking automatically.
An example of such behavior: Windows Media Player, which prevents this while a movie is running.

Known solutions (kinda workarounds):

  1. It is possible to send a Win32 Mouse Move event every fixed interval of time (for example, every minute)
  2. It is possible to send a key to the program (for example "Left Shift" key up) every fixed interval of time (for example, every minute)

QUESTION:
How can I prevent windows workstation from locking without using these workarounds?

Disclaimer:
I was pretty sure, there should be a similar question answered somewhere on StackOverflow, but i didn't find any. I would appreciate, if you could point me into the right direction.

like image 691
Alexander Yezutov Avatar asked Dec 22 '11 07:12

Alexander Yezutov


People also ask

Do applications run in the background when the machine is locked?

As Cyber_Defend_Team pointed out, the applications are running background when the machine is locked. But the processor will be suspended when the machine goes to sleep mode and the machine's state is stored in the RAM. It is by design and it is not configurable.

Will desktop flows work on a locked machine?

The link you shared seems to provide the answer in that Desktop Flows will not work on a locked machine (see screenshot below). Thank for the nudge. I propose you add a new thread with any question outside of the specific issue addressed here. Hey @StehtBI, you'll need to turn on a setting on Power Automate Machines

What happens to my computer when it is locked?

When PC is locked, you application would run in background and you are login. However if you log off or put your PC in sleep mode, then your application and processor will be suspended. Do you mean the machine got locked with screensaver or it goes to sleep mode?


1 Answers

The solution has been pointed out through the comments, but I'm providing a simple starter solution for anyone else arriving via a web search:

/// <summary> /// Interaction logic for App.xaml /// </summary> public partial class App : Application {     [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]     static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);      public App()     {         InitializeComponent();          App.Current.Startup += new StartupEventHandler((sender, e) =>             {                 SetThreadExecutionState(EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS);             });         App.Current.Exit += new ExitEventHandler((sender, e) =>             {                 SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);             });     } }  [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 } 

An alternative place to put the logic would be within an event handler for StateChanged on your main application window:

this.StateChanged += new EventHandler((sender, e) =>     {         if (WindowState == System.Windows.WindowState.Maximized)         {             SetThreadExecutionState(EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS);         }         else         {             SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);         }     }); 
like image 136
Mike Guthrie Avatar answered Sep 20 '22 19:09

Mike Guthrie