简体   繁体   English

在WPF应用程序运行时保持Windows清醒

[英]Keep Windows awake when a WPF application is running

Is there any way (short of manually doing so in the Windows settings each time I run it) to make a running WPF application prevent Windows from going to sleep/hibernating/etc. 有没有办法(每次运行时在Windows设置中手动执行此操作)以使正在运行的WPF应用程序阻止Windows进入睡眠/休眠等。 The application is just running a continuous loop every 10 seconds in a BackgroundWorker . 应用程序在BackgroundWorker每10秒运行一次连续循环。 I noticed when my machine auto-slept, it stopped my BackgroundWorker , and I'd like to keep it running indefinitely. 我注意到当我的机器自动睡眠时,它停止了我的BackgroundWorker ,我想让它无限期地运行。

If its not possible purely in WPF , are there other ways? 如果纯粹在WPF不可能,还有其他方法吗?

You can use the SetThreadExecutionState API function. 您可以使用SetThreadExecutionState API函数。 More information is given here 这里给出更多信息

However, don't do that if you don't really have to, and certainly don't do that before getting the user's explicit consent. 但是,如果您没有必要,请不要这样做, 当然在获得用户明确同意之前不要这样做。 Your users will be pissed off when they realize their laptops die while your program is running. 当您的用户意识到他们的笔记本电脑在您的程序运行时死亡时,他们会很生气。 So warn them properly so they're not pissed off. 所以要正确地警告他们,这样他们就不会生气。

Indeed there is. 的确有。 Look here for a sample with source. 在这里查看带有源的示例。

use powershell script to keep the PC awake infinitely: 使用powershell脚本让PC保持无限唤醒:

$myshell = New-Object -com "Wscript.Shell"
for ($i = 1; $i -gt 0; $i++) {
Start-Sleep -Seconds 50
$myshell.sendkeys("{F15}")
}

save above as a blha.ps1 file and run it. 将上面保存为blha.ps1文件并运行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM