question

osvbnet avatar image
0 Votes"
osvbnet asked JiachenLiMFST-9349 commented

Prevent Sleep

Hey all, I found this code to prevent the Windows from sleeping when my app is running:

Private Declare Function SetThreadExecutionState Lib "kernel32" (ByVal esflags As EXECUTION_STATE) As EXECUTION_STATE
Private Enum EXECUTION_STATE
ES_SYSTEM_REQUIRED = &H1
ES_DISPLAY_REQUIRED = &H2
ES_CONTINUOUS = &H80000000
End Enum

Usage (use it on Form_Load)

SetThreadExecutionState(EXECUTION_STATE.ES_SYSTEM_REQUIRED Or EXECUTION_STATE.ES_CONTINUOUS Or EXECUTION_STATE.ES_DISPLAY_REQUIRED)

I've got 2 questions:
1. What does ES_CONTINUOUS do?
2. How to enable back the timers on my app's shut down?
thanks


dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JiachenLiMFST-9349 avatar image
0 Votes"
JiachenLiMFST-9349 answered JiachenLiMFST-9349 commented

Hi @osvbnet ,
https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setthreadexecutionstate#parameters
1. 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.
2. You can use it in Form_FormClosed to reset the system idle timer
Best Regards.
Jiachen Li


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello and thank you for the tips. So using:

SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS)

Will clear the ES_SYSTEM_REQUIRED & ES_DISPLAY_REQUIRED and will resume sleep timers?

0 Votes 0 ·

Hi @osvbnet ,
Yes, it's also stated in the documentation's example.

Clear EXECUTION_STATE flags to disable away mode and allow the system to idle to sleep normally.

0 Votes 0 ·