【问题标题】:if condition not working as desired如果条件未按预期工作
【发布时间】:2016-08-26 18:48:31
【问题描述】:

编写 VB 脚本来监控电池百分比。如果大于 95,则拔掉插头。如果 else 部分并休眠。它没有进入“系统不会休眠”部分。

hibernate.bat 是一个简单的使 Windows7 休眠的批处理脚本。 代码是一个永无止境的for 循环,其中 do while 循环用于在 shell 脚本中复制“继续”。当电池电量达到 20% 时,插件充电器有 10 秒的时间。如果收费,则必须进行下一个 for 循环迭代。没有进入“如果收费”循环。为什么?

Set oLocator = CreateObject("WbemScripting.SWbemLocator")
Set oServices = oLocator.ConnectServer(".","root\wmi")
Set oResults = oServices.ExecQuery("select * from batteryfullchargedcapacity")
For Each oResult In oResults
    iFull = oResult.FullChargedCapacity
Next

For i=1 To 10
    Do
        i=+2
        Set oResults = oServices.ExecQuery("select * from batterystatus")
        For Each oResult In oResults
            iRemaining = oResult.RemainingCapacity
            Charging = oResult.Charging
            Discharging = oResult.Discharging
        Next

        iPercent = ((iRemaining / iFull) * 100) Mod 100
        If Charging And (iPercent > 95) Then
            MsgBox "Unplug Charger. Battery is at " & iPercent & "%", vbInformation, "Battery monitor"
        ElseIf Discharging And (iPercent < 20) Then
            MsgBox "Plug-in Charger. Battery is at " & iPercent & "%", vbInformation, "Battery monitor"
            WScript.Sleep 10000 ' 10 sec
            If Charging Then
                CreateObject("WScript.Shell").Popup "System won't hibernate", 5, "Good News!!!"
                Exit Do
            Else
                Set shell = CreateObject("WScript.Shell")
                shell.CurrentDirectory = "C:\Users\abcd\Desktop"
                CreateObject("WScript.Shell").Popup "System will hibernate", 5, "Hmm..."
                shell.Run "hibernate.bat"
                Exit For
            End If
        End If
    Loop While False
Next

【问题讨论】:

标签: vbscript


【解决方案1】:

这是你的问题:

ElseIf Discharging And (iPercent < 20) Then
    ...
    If Charging Then
        ...
    Else
        ...
    End If
End If

ChargingDischarging 状态是互逆的。如果Discharging 为真,则Charging 为假,反之亦然。因此,当您进入 ElseIf 分支时,Charging 的值始终 False。而且由于变量的值反映了赋值时的状态,它仍然 False 即使在您插入电源线之后。当电池状态发生变化时,该变量不会自动更新。您需要在延迟后重新评估电池状态,以使您的代码按预期工作。为此,我建议将 WMI 查找重构为两个函数:

Function IsCharging
    For Each oResult In oServices.ExecQuery("SELECT * FROM BatteryStatus")
        IsCharging = oResult.Charging
    Next
End Function

Function GetRemainingCapacity
    For Each oResult In oServices.ExecQuery("SELECT * FROM BatteryStatus")
        GetRemainingCapacity = oResult.RemainingCapacity
    Next
End Function

并像这样使用它们:

Do
    iPercent = ((GetRemainingCapacity / iFull) * 100) Mod 100
    If IsCharging And (iPercent > 95) Then
        ...
    ElseIf Not IsCharging And (iPercent < 20) Then
        ...
        If IsCharging Then
            ...
        Else
            ...
        End If
    End If
Loop While True

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-01
    • 2021-12-13
    • 2014-01-07
    • 2015-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多