Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
738 views
in Technique[技术] by (71.8m points)

performance - C# .NET: How to check if we're running on battery?

i want to be a good developer citizen, pay my taxes, and disable things if we're running over Remote Desktop, or running on battery.

If we're running over remote desktop (or equivalently in a Terminal server session), we must disable animations and double-buffering. You can check this with:

/// <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;
    }
}

Now i need to find out if the user is running on battery power. If they are, i don't want to blow through their battery. i want to do things such as

  • disable animations
  • disable background spell-checking
  • disable background printing
  • turn off gradients
  • use graphics.SmoothingMode = SmoothingMode.HighSpeed;
  • use graphics.InterpolationMode = InterpolationMode.Low;
  • use graphics.CompositingQuality = CompositingQuality.HighSpeed;
  • minimize hard drive access - to avoid spin up
  • minimize network access - to save WiFi power

Is there a managed way to see if the machine is currently running on battery?

Bonus Reading

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I believe you can check SystemInformation.PowerStatus to see if it's on battery or not.

Boolean isRunningOnBattery =
      (System.Windows.Forms.SystemInformation.PowerStatus.PowerLineStatus == 
       PowerLineStatus.Offline);

Edit: In addition to the above, there's also a System.Windows.Forms.PowerStatus class. One of its methods is PowerLineStatus, which will equal PowerLineStatus.Online if it's on AC Power.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...