110,539
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace PowerApp
{
internal enum POWER_ACTION
{
PowerActionNone = 0,
PowerActionReserved,
PowerActionSleep,
PowerActionHibernate,
PowerActionShutdown,
PowerActionShutdownReset,
PowerActionShutdownoff,
PowerActionWarmEject
}
internal enum SYSTEM_POWER_STATE
{
PowerSystemUnspecified=0,
PowerSystemWorking=1,
PowerSystemSleep1=2,
PowerSystemSleep2=3,
PowerSystemSleep3=4,
PowerSystemHibernate=5,
PowerSystemShutdown=6,
PowerSystemMaximum=7
}
internal struct POWER_ACTION_POLICY
{
POWER_ACTION Action;
ulong Flags;
ulong EventCode;
}
internal struct MACHINE_POWER_POLICY
{
ulong Revision;
SYSTEM_POWER_STATE MinSleepAc;
SYSTEM_POWER_STATE MinSleepDc;
SYSTEM_POWER_STATE ReducedLatencySleepAc;
SYSTEM_POWER_STATE ReducedLatencySleepDc;
public ulong DozeTimeoutAc;
public ulong DozeTimeoutDc;
ulong DozeS4TimeoutAc;
ulong DozeS4TimeoutDc;
ulong MinThrottleAc;
char MinThrottleDc;
// UCHAR pad1[2];
POWER_ACTION_POLICY OverThrottledAc;
POWER_ACTION_POLICY OverThrottledDc;
}
internal struct USER_POWER_POLICY
{
ulong Revision;
POWER_ACTION_POLICY IdleAc;
POWER_ACTION_POLICY IdleDc;
int IdleTimeoutAc;
int IdleTimeoutDc;
char IdleSensitivityAc;
char IdleSensitivityDc;
char ThrottlePolicyAc;
char ThrottlePolicyDc;
SYSTEM_POWER_STATE MaxSleepAc;
SYSTEM_POWER_STATE MaxSleepDc;
//ulong Reserved[2];
public int VideoTimeoutAc;
public int VideoTimeoutDc;
public int SpindownTimeoutAc;
int SpindownTimeoutDc;
Boolean OptimizeForPowerAc;
Boolean OptimizeForPowerDc;
char FanThrottleToleranceAc;
char FanThrottleToleranceDc;
char ForcedThrottleAc;
char ForcedThrottleDc;
}
internal struct POWER_POLICY
{
public USER_POWER_POLICY user;
public MACHINE_POWER_POLICY mach;
}
class Monitor
{
[DllImport("powrprof.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern bool GetActivePwrScheme(ref uint puiID);
[DllImport("powrprof.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern bool ReadPwrScheme(uint uiID, out POWER_POLICY pPowerPolicy);
[DllImport("powrprof.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern bool WritePwrScheme(ref uint puiID, string lpszName, string lpszDescription, ref POWER_POLICY pPowerPolicy);
public int MonitorPerform()
{
int acTime=0;
uint uiID = 0;
POWER_POLICY PwrPolicy=new POWER_POLICY();
GetActivePwrScheme(ref uiID);
ReadPwrScheme(uiID, out PwrPolicy);
acTime = PwrPolicy.user.VideoTimeoutDc;
return acTime;
}
}
}