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!
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
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:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PowerManagement.DisablePowerSaving()
PowerManagement.EnablePowerSaving()
End Sub
End Class
Public Class PowerManagement
Private Sub New()
End Sub
Public Shared Function DisablePowerSaving() As EXECUTION_STATE
Return NativeMethods.SetThreadExecutionState(EXECUTION_STATE.ES_SYSTEM_REQUIRED _
Or EXECUTION_STATE.ES_DISPLAY_REQUIRED _
Or EXECUTION_STATE.ES_CONTINUOUS)
End Function
Public Shared Function EnablePowerSaving() As EXECUTION_STATE
Return NativeMethods.SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS)
End Function
Private Class NativeMethods
Friend Declare Function SetThreadExecutionState Lib "kernel32" (ByVal esFlags As EXECUTION_STATE) As EXECUTION_STATE
End Class
<Flags()>
Public Enum EXECUTION_STATE As Integer
ES_CONTINUOUS = &H80000000
ES_USER_PRESENT = &H4
ES_DISPLAY_REQUIRED = &H2
ES_SYSTEM_REQUIRED = &H1
End Enum
End Class
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....??