Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27386

PInvoke stack imbalance when using SetThreadExecutionState

$
0
0
I have a tablet that runs a virtual dash for my drag car. While the software is running, I want to prevent any sleep mode, so I added this code to the top of my vb and I call the function "PowerSaveOff" as soon as the form loads. In .Net 3.5, the MDA for PInvoke is disabled by default, but in 4.0 I get the error for stack imbalance. Any ideas how to correct this properly rather than using a band-aid and just disabling the exception?

Code:

    Private Declare Function SetThreadExecutionState Lib "kernel32" (ByVal esFlags As Long) As Long
    Public Enum EXECUTION_STATE As Integer

        ES_CONTINUOUS = &H80000000
        ES_DISPLAY_REQUIRED = &H2
        ES_SYSTEM_REQUIRED = &H1
        ES_AWAYMODE_REQUIRED = &H40

    End Enum

    Public Shared Sub PowerSaveOff()
        SetThreadExecutionState(EXECUTION_STATE.ES_SYSTEM_REQUIRED Or EXECUTION_STATE.ES_DISPLAY_REQUIRED Or EXECUTION_STATE.ES_CONTINUOUS)
    End Sub

    Public Shared Sub PowerSaveOn()
        SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS)
    End Sub


Viewing all articles
Browse latest Browse all 27386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>