Results 1 to 12 of 12

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

Threaded View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2013
    Location
    Albania
    Posts
    101

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

    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
    Last edited by kevinKZzaka; Aug 6th, 2013 at 07:14 PM.

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