C#.NET:如何检查我们是否使用电池运行?

Ian*_*oyd 27 c# optimization performance

如果我们通过远程桌面运行或使用电池运行,我想成为一名优秀的开发者公民,支付我的税款并禁用一些东西.

如果我们在远程桌面上运行(或等效于终端服务器会话),我们必须禁用动画和双缓冲.您可以通过以下方式检查:

/// <summary>
/// Indicates if we're running in a remote desktop session.
/// If we are, then you MUST disable animations and double buffering i.e. Pay your taxes!
/// 
/// </summary>
/// <returns></returns>
public static Boolean IsRemoteSession
{
    //This is just a friendly wrapper around the built-in way
    get
    {
        return System.Windows.Forms.SystemInformation.TerminalServerSession;
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我需要找出用户是否使用电池供电.如果他们是,我不想吹他们的电池.我想做的事情如

  • 禁用动画
  • 禁用后台拼写检查
  • 禁用后台打印
  • 关闭渐变
  • 使用 graphics.SmoothingMode = SmoothingMode.HighSpeed;
  • 使用 graphics.InterpolationMode = InterpolationMode.Low;
  • 使用 graphics.CompositingQuality = CompositingQuality.HighSpeed;
  • 最小化硬盘访问 - 避免旋转
  • 最小化网络访问 - 节省WiFi功率

是否有管理方式来查看机器当前是否使用电池运行?

奖金阅读

Pow*_*ord 32

我相信你可以查看SystemInformation.PowerStatus以查看它是否正在使用电池.

Boolean isRunningOnBattery =
      (System.Windows.Forms.SystemInformation.PowerStatus.PowerLineStatus == 
       PowerLineStatus.Offline);
Run Code Online (Sandbox Code Playgroud)

编辑:除了上面,还有一个System.Windows.Forms.PowerStatus类.其中一种方法是PowerLineStatus,如果它在交流电源上,它将等于PowerLineStatus.Online.


Ian*_*oyd 8

R. Bemrose找到了托管电话.这是一些示例代码:

/// <summary>
/// Indicates if we're running on battery power.
/// If we are, then disable CPU wasting things like animations, background operations, network, I/O, etc
/// </summary>
public static Boolean IsRunningOnBattery
{
   get
   {
      PowerLineStatus pls = System.Windows.Forms.SystemInformation.PowerStatus.PowerLineStatus;

      //Offline means running on battery
      return (pls == PowerLineStatus.Offline);
   }
}
Run Code Online (Sandbox Code Playgroud)

  • 请返回pls == PowerLineStatus.Offline; 这伤害了我的眼睛. (6认同)
  • 不,他说帮助函数很好,但是你的代码有if(blah)的反模式{return true; } else {return false; 相反,它应该只是返回(等等); (6认同)
  • @Basic 我只提及编辑,以防有人看到答案并对评论感到困惑。答案已根据原来的内容进行了编辑。我不想将其编辑回来,因为有些人在编辑内容时会变得敏感。 (2认同)

dri*_*iis 7

您可以使用P/Invoke使用GetSystemPowerStatus函数.请参阅:http: //msdn.microsoft.com/en-gb/library/aa372693.aspx

这是一个例子:

using System;
using System.Runtime.InteropServices;
namespace PowerStateExample
{
    [StructLayout(LayoutKind.Sequential)]
    public class PowerState
    {
        public ACLineStatus ACLineStatus;
        public BatteryFlag BatteryFlag;
        public Byte BatteryLifePercent;
        public Byte Reserved1;
        public Int32 BatteryLifeTime;
        public Int32 BatteryFullLifeTime;

        // direct instantation not intended, use GetPowerState.
        private PowerState() {}

        public static PowerState GetPowerState()
        {
            PowerState state = new PowerState();
            if (GetSystemPowerStatusRef(state))
                return state;

            throw new ApplicationException("Unable to get power state");
        }

        [DllImport("Kernel32", EntryPoint = "GetSystemPowerStatus")]
        private static extern bool GetSystemPowerStatusRef(PowerState sps);
    }

    // Note: Underlying type of byte to match Win32 header
    public enum ACLineStatus : byte
    {
        Offline = 0, Online = 1, Unknown = 255
    }

    public enum BatteryFlag : byte
    {
        High = 1, Low = 2, Critical = 4, Charging = 8,
        NoSystemBattery = 128, Unknown = 255
    }

    // Program class with main entry point to display an example.
    class Program
    {        
        static void Main(string[] args)
        {
            PowerState state = PowerState.GetPowerState();
            Console.WriteLine("AC Line: {0}", state.ACLineStatus);
            Console.WriteLine("Battery: {0}", state.BatteryFlag);
            Console.WriteLine("Battery life %: {0}", state.BatteryLifePercent);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)