防止 Windows 工作站(桌面)在运行 WPF 程序时锁定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8600627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Prevent Windows workstation (desktop) from locking while running a WPF program
提问by Alexander Yezutov
Issue:
I have a WPF fullscreenapplication, which acts as a dashboard. The computer is in domain and domain policies enforce the computer to be locked in 10 minutes after the last user activity. I want to prevent the workstation (or desktop) from locking automatically.
An example of such behavior: Windows Media Player, which prevents this while a movie is running.
问题:
我有一个 WPF全屏应用程序,它充当仪表板。计算机在域中,域策略强制计算机在最后一次用户活动后 10 分钟内被锁定。我想防止工作站(或桌面)自动锁定。
此类行为的一个示例:Windows Media Player,它可以在电影运行时防止出现这种情况。
Known solutions (kinda workarounds):
已知的解决方案(有点变通方法):
- It is possible to send a Win32 Mouse Move eventevery fixed interval of time (for example, every minute)
- It is possible to send a key to the program (for example "Left Shift" key up) every fixed interval of time (for example, every minute)
- 可以每隔固定的时间间隔(例如,每分钟)发送一个 Win32 鼠标移动事件
- 可以每隔固定的时间间隔(例如,每分钟)向程序发送一个键(例如“左移”键向上)
QUESTION:
How can I prevent windows workstation from locking without using these workarounds?
问题:
如何在不使用这些解决方法的情况下防止 Windows 工作站锁定?
Disclaimer:
I was pretty sure, there should be a similar question answered somewhere on StackOverflow, but i didn't find any. I would appreciate, if you could point me into the right direction.
免责声明:
我很确定,应该在 StackOverflow 的某处回答类似的问题,但我没有找到。如果您能指出我正确的方向,我将不胜感激。
采纳答案by Mike Guthrie
The solution has been pointed out through the comments, but I'm providing a simple starter solution for anyone else arriving via a web search:
该解决方案已通过评论指出,但我正在为通过网络搜索到达的其他人提供一个简单的入门解决方案:
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
public App()
{
InitializeComponent();
App.Current.Startup += new StartupEventHandler((sender, e) =>
{
SetThreadExecutionState(EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS);
});
App.Current.Exit += new ExitEventHandler((sender, e) =>
{
SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);
});
}
}
[FlagsAttribute]
public enum EXECUTION_STATE : uint
{
ES_AWAYMODE_REQUIRED = 0x00000040,
ES_CONTINUOUS = 0x80000000,
ES_DISPLAY_REQUIRED = 0x00000002,
ES_SYSTEM_REQUIRED = 0x00000001
// Legacy flag, should not be used.
// ES_USER_PRESENT = 0x00000004
}
An alternative place to put the logic would be within an event handler for StateChanged
on your main application window:
放置逻辑的另一个位置是在StateChanged
主应用程序窗口的事件处理程序中:
this.StateChanged += new EventHandler((sender, e) =>
{
if (WindowState == System.Windows.WindowState.Maximized)
{
SetThreadExecutionState(EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS);
}
else
{
SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);
}
});