2016-09-26 62 views

回答

0

該方法我採取是P /調用和檢查裝置的電源狀態,所以我創建了一個名爲電源狀態,類:

public enum ACLineStatus { Offline = 0, Online = 1, Unknown = 255 } 
[Flags] 
public enum BatteryStatus { High = 1, Low = 2, Critical = 4, Charging = 8, NoSystemBattery = 128, Unknown =-1 } 

public class PowerStatus 
{ 
    private struct _SYSTEM_POWER_STATUS_EX 
    { 
     public byte ACLineStatus; 
     public byte BatteryFlag; 
     public byte BatteryLifePercent; 
     public byte Reserved1; 
     public uint BatteryLifeTime; 
     public uint BatteryFullLifeTime; 
     public byte Reserved2; 
     public byte BackupBatteryFlag; 
     public byte BackupBatteryLifePercent; 
     public byte Reserved3; 
     public uint BackupBatteryLifeTime; 
     public uint BackupBatteryFullLifeTime; 
    } 

    [DllImport("coredll")] 
    private static extern uint GetSystemPowerStatusEx(ref _SYSTEM_POWER_STATUS_EX lpSystemPowerStatus, 
     bool fUpdate); 

    public ACLineStatus ACLineStatus { get; set; } 
    public BatteryStatus BatteryStatus { get; set; } 
    public byte? BatteryLifePercent { get; set; } 
    public TimeSpan? BatteryLifeTime { get; set; } 
    public TimeSpan? BatteryFullLifeTime { get; set; } 
    public BatteryStatus BackupBatteryStatus { get; set; } 
    public byte? BackupBatteryLifePercent { get; set; } 
    public TimeSpan? BackupBatteryLifeTime { get; set; } 
    public TimeSpan? BackupBatteryFullLifeTime { get; set; } 

    private PowerStatus() 
    { 

    } 

    public static PowerStatus GetCurrent() 
    { 
     _SYSTEM_POWER_STATUS_EX powerStatus = new _SYSTEM_POWER_STATUS_EX(); 
     GetSystemPowerStatusEx(ref powerStatus, true); 

     return new PowerStatus 
     { 
      ACLineStatus = (ACLineStatus)powerStatus.ACLineStatus, 
      BatteryStatus = (BatteryStatus)powerStatus.BatteryFlag, 
      BatteryLifePercent = powerStatus.BatteryLifePercent.Between((byte)0, (byte)100) ? (byte?)powerStatus.BatteryLifePercent : null, 
      BatteryLifeTime = powerStatus.BatteryLifeTime >= 0 ? new TimeSpan(0, 0, (int)powerStatus.BatteryLifeTime) as TimeSpan? : null, 
      BatteryFullLifeTime = powerStatus.BatteryFullLifeTime >= 0 ? new TimeSpan(0, 0, (int)powerStatus.BatteryFullLifeTime) as TimeSpan? : null, 
      BackupBatteryStatus = (BatteryStatus)powerStatus.BackupBatteryFlag, 
      BackupBatteryLifePercent = powerStatus.BackupBatteryLifePercent.Between((byte)0, (byte)100) ? (byte?)powerStatus.BackupBatteryLifePercent : null, 
      BackupBatteryLifeTime = powerStatus.BackupBatteryLifeTime >= 0 ? new TimeSpan(0, 0, (int)powerStatus.BackupBatteryLifeTime) as TimeSpan? : null, 
      BackupBatteryFullLifeTime = powerStatus.BackupBatteryFullLifeTime >= 0 ? new TimeSpan(0, 0, (int)powerStatus.BackupBatteryFullLifeTime) as TimeSpan? : null 
     }; 
    } 
} 

,然後在我的「設備」類我有一個計時器,其火災時,該設備是停靠

public static event EventHandler DeviceDocked; 

    private static ACLineStatus _lastPowerState = PowerStatus.GetCurrent().ACLineStatus; 
    private static void timer_Tick(object state) 
    { 
     ACLineStatus powerState = PowerStatus.GetCurrent().ACLineStatus; 
     _timer.Change(Timeout.Infinite, Timeout.Infinite); 
     if (powerState == ACLineStatus.Online && _lastPowerState == ACLineStatus.Offline) 
     { 
      var e = DeviceDocked; 
      if (e != null) 
      { 
       e(null, EventArgs.Empty); 
      } 
     } 
     _lastPowerState = powerState; 
     _timer.Change(1000, Timeout.Infinite); 
    } 

PowerStatus.GetCurrent()可能是你需要一個事件。我希望這有助於:)

相關問題