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;
是否有管理方式来查看机器当前是否使用电池运行?
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.
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)
您可以使用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)