How to Prevent Windows from Entering Idle State

Preventing and Re-enabling Windows Idle State

How about saving the return value of your call to SetThreadExecutionState? The documentation says:

If the function succeeds, the return value is the previous thread execution state.

So it seems reasonable that you could write:

private EXECUTION_STATE SavedState;

SavedState = SleepUtil.SetThreadExecutionState(...);

SavedState contains the previous value. So it seems like you could put things back by writing:

SleepUtil.SetThreadExecutionState(SavedState);

You could even examine the return value (SavedState) to determine what the default is, and use that ...

At least, that's what I'd try first.

C# uwp application prevent idle mode

You can use this:

var displayRequest = new Windows.System.Display.DisplayRequest();
displayRequest.RequestActive();

But be aware of the fact that you are just requesting the screen to stay active. This can not prevent the user from locking the screen by himself manually.

How to prevent windows from going to sleep when my c++ application is running?

SetThreadExecutionState function

Enables an application to inform the system that it is in use, thereby preventing the system from entering sleep or turning off the display while the application is running.

Read more about the API here: SetThreadExecutionState

Example:

// The following sets the appropriate flags to prevent system to go into sleep mode.
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_AWAYMODE_REQUIRED);

// This clears the flags and allows the system to sleep normally.
SetThreadExecutionState(ES_CONTINUOUS);

Prevent a computer from entering Sleep/Standby/Hibernate while program is running

You need to specify ES_CONTINUOUS as well, otherwise you only reset the idle timer once.

Per the documentation:

Calling SetThreadExecutionState without ES_CONTINUOUS simply resets the idle timer; to keep the display or system in the working state, the thread must call SetThreadExecutionState periodically.

SetThreadExecutionState(EXECUTION_STATE.ES_SYSTEM_REQUIRED Or EXECUTION_STATE.ES_CONTINUOUS)

If you at a later point wish to undo this and make the system able to put itself to sleep again, call the function and specify only ES_CONTINUOUS.

C#: How to prevent a laptop from going into Stand-By

I think you'll have to P/Invoke. But don't be scared... it's pretty easy.

SetThreadExecutionState is your friend. It's available in the PInvoke project, also available on NuGet.

Easiest way to simulate user action to prevent system being considered idle

After a lot of playing around, I came up with a solution that worked and I'm answering this question so as to help anyone out there that may be looking for the same information.

(Coded in VB):

In Declarations Section:

Private Declare Function SendInput Lib "user32.dll" (ByVal nInputs As Integer, ByRef pInputs As INPUT_TYPE, ByVal cbSize As Integer) As Integer
Const INPUT_MOUSE = 0
Const MOUSEEVENTF_MOVE = &H1

Public Structure MOUSEINPUT
Public dx As Integer
Public dy As Integer
Public mouseData As Integer
Public dwFlags As Integer
Public dwtime As Integer
Public dwExtraInfo As Integer
End Structure

Public Structure INPUT_TYPE
Public dwType As Integer
Public xi As MOUSEINPUT
End Structure

The Sub to move the mouse:

Public Sub MoveMouse(ByVal x As Integer, ByVal y As Integer, ByVal flag As Integer)

Dim inputEvents As INPUT_TYPE
Dim xi As New MOUSEINPUT

xi.dx = x
xi.dy = y
xi.mouseData = 0
xi.dwtime = 0
xi.dwFlags = flag
xi.dwExtraInfo = 0

inputEvents.dwType = INPUT_MOUSE
inputEvents.xi = xi

SendInput(1, inputEvents, Len(inputEvents))
End Sub

The Code in the Timer_Tick Event:

Private Sub Updater_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Updater.Tick

' Move the mouse (relatively) 1 pixel away and then back
MoveMouse(1, 1, MOUSEEVENTF_MOVE)
MoveMouse(-1, -1, MOUSEEVENTF_MOVE)

End Sub

I tested this using the GetLastInputInfo API function and it works!!

... The most helpful link I found was this one:
http://homeofcox-cs.blogspot.com/2008/07/c-simulate-mouse-and-keyboard-events.html

Thank you all for all your help and I hope this helps other people too!!



Related Topics



Leave a reply



Submit