Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21271

[RESOLVED] Loop to detect when battery power status changes?

$
0
0
I want to detect when the battery power status of a PC (ex.: a laptop) changes. I am using GetSystemPowerStatus API to get battery life percentage. What I want to do is to detect if the battery life percentage changes. The easy way to do this would be using a timer but that would lag my app. I was thinking about a 'recursive' loop.
It would go kind of like this:

Code:

Do Until batteryLifePercentageChanges
checkIfBatteryLifePercentageChanges
Loop

and then return the new battery life percentage.

Here is the code to find the battery power percentage:

Code:

Private Declare Function GetSystemPowerStatus Lib "kernel32" (lpSystemPowerStatus As _
SYSTEM_POWER_STATUS) As Long

Private Type SYSTEM_POWER_STATUS
        ACLineStatus As Byte
        BatteryFlag As Byte
        BatteryLifePercent As Byte
        Reserved1 As Byte
        BatteryLifeTime As Long
        BatteryFullLifeTime As Long
End Type

Private Sub Command1_Click()
Dim MyStatus As SYSTEM_POWER_STATUS
Dim MyLifeTime As Double
GetSystemPowerStatus MyStatus
MyLifeTime = MyStatus.BatteryLifeTime
If MyLifeTime = -1 Then
    MsgBox "Using AC Power"
Else
    MsgBox "I have " & MyLifeTime / 3600 & " hours remaining on battery."
End If
End Sub

I am not really good with loops. They can really confuse me sometimes. For example I remember once that when I was working with loops a lot, I asked myself "How does a loop know when to begin?" and stupid stuffs like that. LoL

Any help would be appreciated. Thank you :)

Viewing all articles
Browse latest Browse all 21271

Trending Articles



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