Results 1 to 4 of 4

Thread: Ensure "Sleep" is disabled

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    12

    Ensure "Sleep" is disabled

    Is there any easy way to check if sleep mode is disabled as my program is loading? Ideally I would have the program automatically disable the setting as well.

    Reason for this is because the PCI card I'm using loses connection when sleep is initiated and the only way to reconnect that I'm aware of is to restart.

    Many Thanks!

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Ensure "Sleep" is disabled

    To disable sleep mode while your app is running you can try using SetThreadExecutionState

    You should be able to find some examples, like this...
    Code:
    <System.Runtime.InteropServices.DllImport("kernel32.dll")> _
    Private Shared Function SetThreadExecutionState(ByVal newExecutionState As ExecutionState) As ExecutionState
    End Function
    
    <Flags()> _
    Public Enum ExecutionState As UInteger
    	ES_SYSTEM_REQUIRED = 1UI
    	ES_DISPLAY_REQUIRED = 2UI
    	ES_USER_PRESENT = 4UI
    	ES_CONTINUOUS = &H80000000UI
    End Enum
    
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    	' disable sleep modes on load (no action needed on exit)
    	SuspendSleep()
    End Sub
    
    Private Shared Sub SuspendSleep()
    	' This will prevent system entering sleep state due to inactivity.
    	' The user can still shutdown if they want to though.
    	Dim oldState As ExecutionState = SetThreadExecutionState(ExecutionState.ES_CONTINUOUS Or ExecutionState.ES_DISPLAY_REQUIRED Or ExecutionState.ES_SYSTEM_REQUIRED)
    End Sub
    
    Private Shared Sub SetExecutionStateBackToNormal()
    	' When your app closes, things reset to normal anyway as this thread ends.
    	' You can send just es_continuous to reset whilst your app is still running.
    	SetThreadExecutionState(ExecutionState.ES_CONTINUOUS)
    End Sub

  3. #3
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Ensure "Sleep" is disabled

    Pretty much the same answer as Edgemeal but .Net is all about reusability. (That and I had this typed up before getting distracted with work)

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    4.         PowerManagement.DisablePowerSaving()
    5.  
    6.         PowerManagement.EnablePowerSaving()
    7.     End Sub
    8.  
    9. End Class
    10.  
    11. Public Class PowerManagement
    12.  
    13.     Private Sub New()
    14.     End Sub
    15.  
    16.     Public Shared Function DisablePowerSaving() As EXECUTION_STATE
    17.         Return NativeMethods.SetThreadExecutionState(EXECUTION_STATE.ES_SYSTEM_REQUIRED _
    18.                                                      Or EXECUTION_STATE.ES_DISPLAY_REQUIRED _
    19.                                                      Or EXECUTION_STATE.ES_CONTINUOUS)
    20.     End Function
    21.  
    22.     Public Shared Function EnablePowerSaving() As EXECUTION_STATE
    23.         Return NativeMethods.SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS)
    24.     End Function
    25.  
    26.     Private Class NativeMethods
    27.         Friend Declare Function SetThreadExecutionState Lib "kernel32" (ByVal esFlags As EXECUTION_STATE) As EXECUTION_STATE
    28.     End Class
    29.  
    30.     <Flags()>
    31.     Public Enum EXECUTION_STATE As Integer
    32.         ES_CONTINUOUS = &H80000000
    33.         ES_USER_PRESENT = &H4
    34.         ES_DISPLAY_REQUIRED = &H2
    35.         ES_SYSTEM_REQUIRED = &H1
    36.     End Enum
    37. End Class
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    12

    Re: Ensure "Sleep" is disabled

    Thanks! The SetThreadExecutionState function will help immensely.

    Now is there a way for this to persist if the application is closed? The PC that this program runs on is dedicated for a single industrial operation and is dependent on the connection to the PCI card. If a local IT person turns on sleep mode and then goes home for the day, second and third shift will have problems, there may be stoppages of our production lines and I will be getting a call saying my program is broken at 3am. I say to restart and then all is well. I cannot guarantee my program will be always running to make use of the SetThreadExectionState function to ensure sleep does not occur.

    Maybe I should also be looking into a way to rescan the PCI slots to fix the lost connection in the first place....??

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width