我有一个数字标牌应用程序,我想在计算机上优先运行。我现在已经连接到win32事件(ES_DISPLAY_REQUIRED)以防止这种情况发生。但是,我想摆脱这个win32调用,因为我认为这是一个提升我的应用程序的权限,以便它需要以管理员身份运行。
是否有管理方式做同样的事情?我的意思是,什么是媒体播放器等在观看电影时阻止睡眠/屏幕保护?
答案 0 :(得分:0)
您可以尝试使用SetThreadExecutionState函数,如下所示:
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern uint SetThreadExecutionState(uint esFlags);
public const uint ES_CONTINUOUS = 0x80000000;
public const uint ES_SYSTEM_REQUIRED = 0x00000001;
public const uint ES_AWAYMODE_REQUIRED = 0x00000040;
public void AllowStandbyMode(bool allow)
{
if (allow)
{
// Set state back to normal
SetThreadExecutionState(ES_CONTINUOUS);
}
else
{
// Try this for vista, it will fail on XP
if (SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_AWAYMODE_REQUIRED) == 0)
{
// Try XP variant as well just to make sure
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED);
}
}
}