none
How to stop system Sleeping RRS feed

  • Question

  • I market a windows 10 vb.net racing gambling app with a SetAndForget mode. In this mode all bets are placed automatically by the app just before races are run. The mechanism for doing this is a windows forms app running background threads. I need to be able to stop the system going into Sleep mode while the app is running or, at the very least, stop it from starting if the system is likely to go to Sleep whilst the app is running.

    Does anyone know how I achieve this?

    Tuesday, June 12, 2018 8:16 AM

Answers

  • I tested on Windows 10, VS 2015, and it works for me : If I set sleeping/screen to 1 min and I call

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

    nothing happens after 1 mn

    then if I call

    SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS)
    it goes to sleep after 1 mn...


    • Edited by Castorix31 Wednesday, June 13, 2018 7:59 AM
    • Proposed as answer by Stanly Fan Thursday, June 21, 2018 6:03 AM
    • Marked as answer by trogan Thursday, June 21, 2018 8:20 AM
    Wednesday, June 13, 2018 7:59 AM

All replies

  • With SetThreadExecutionState (P/Invoke)

    Tuesday, June 12, 2018 8:52 AM
  • Thx. What is P/Invoke? 

    I used:

       <FlagsAttribute()>
        Public Enum EXECUTION_STATE As UInteger
            ES_SYSTEM_REQUIRED = &H1
            ES_DISPLAY_REQUIRED = &H2
            ES_CONTINUOUS = &H80000000UI
        End Enum

        <DllImport("Kernel32.DLL", CharSet:=CharSet.Auto, SetLastError:=True)>
        Private Shared Function SetThreadExecutionState(ByVal state As EXECUTION_STATE) As EXECUTION_STATE
        End Function


        Private Sub PowerStatusBrowserForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            SetThreadExecutionState(EXECUTION_STATE.ES_DISPLAY_REQUIRED)
        End Sub

    Note: unintuitively,   SetThreadExecutionState(EXECUTION_STATE.ES_SYSTEM_REQUIRED) does not work?

    Can you also tell me how to save and restore the current system execution state? And what happens if I set it multiple times in the program?


    • Edited by trogan Tuesday, June 12, 2018 9:13 AM
    Tuesday, June 12, 2018 9:13 AM
  • You block with :

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

    and unblock with

    SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS)
    (I tested 3 times with a boolean, on Windows 10 )

    Tuesday, June 12, 2018 9:52 AM
  • Hi troga,

    Here is one sample about preventing your computer  from going to sleep during long running processes.

    https://www.codeproject.com/Articles/290253/Give-your-computer-Sleep-Apnea-Dont-let-it-go-to-s

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 13, 2018 2:50 AM
    Moderator
  • Makes NO sense.


    Wednesday, June 13, 2018 6:21 AM
  • In any case, it doesn't work. Which means I need to check the Sleep setting and tell the user if the system is likely to go to Sleep whilst their App is running. Cannot find out how to do this. Can anyone tell me how?

    Wednesday, June 13, 2018 7:17 AM
  • I tested on Windows 10, VS 2015, and it works for me : If I set sleeping/screen to 1 min and I call

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

    nothing happens after 1 mn

    then if I call

    SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS)
    it goes to sleep after 1 mn...


    • Edited by Castorix31 Wednesday, June 13, 2018 7:59 AM
    • Proposed as answer by Stanly Fan Thursday, June 21, 2018 6:03 AM
    • Marked as answer by trogan Thursday, June 21, 2018 8:20 AM
    Wednesday, June 13, 2018 7:59 AM
  • Using the above settings, my system kept executing for about half and hour and then went to sleep. No good for me, I need it to stay awake indefinitely.

    Surely vast numbers of other people are in the same boat. Maybe it is my Intel NUC system that is causing the problem. Whatever, I cannot market my product until a solution is found.


    • Edited by trogan Wednesday, June 13, 2018 9:53 AM
    Wednesday, June 13, 2018 9:52 AM
  • It appears that I must call SetThreadExecution state "periodically" although periodically is not defined. Do I also need to call it on a thread by thread basis? Looks like it.
    Wednesday, June 13, 2018 10:10 AM
  • Have tried calling SetThreadExecution state "periodically" and it does not work. The system goes to sleep after about 20 mins irrespective of how often I set the state. This needs fixing MIcrosoft (are you listening any more) as it has implications for many, many programs.
    Wednesday, June 13, 2018 11:14 AM
  • Now working ok. May have had a bug in my code. Sorry for false alarm. 
    Wednesday, June 13, 2018 1:25 PM
  • Hi trogan,

    Please remember to close your thread by marking the helpful post as answer, it is beneficial to other community members who face the same issue.

    Thanks for your understanding.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 19, 2018 7:03 AM
    Moderator