问题描述
我写的代码,需要运行只有当没有人类活动在PC上,就像当屏幕保护程序运行。任何关于如何在Windows下的c ++中执行此操作的建议?
I'm writing code that need to run only when there is no human activity on the PC, like when the screensaver is running. Any suggestions on how to do this in c++ under windows?
@talnicolas,只是使用未使用的资源,人们离开计算机多少次, ?
@talnicolas, simply to use unused resources, how many times people leave the computer on but they are in another place?
推荐答案
您可以使用,以检查用户闲置多长时间或在键盘上输入内容)和,以检查屏幕保护程序是否处于活动状态。
You can use GetLastInputInfo
to check how long the user has been idle (not moved around the mouse or typed something on the keyboard) and SystemParametersInfo
to check if a screensaver is active.
#define WINDOWS_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>
// do something after 10 minutes of user inactivity
static const unsigned int idle_milliseconds = 60*10*1000;
// wait at least an hour between two runs
static const unsigned int interval = 60*60*1000;
int main() {
LASTINPUTINFO last_input;
BOOL screensaver_active;
// main loop to check if user has been idle long enough
for (;;) {
if ( !GetLastInputInfo(&last_input)
|| !SystemParametersInfo(SPI_GETSCREENSAVERACTIVE, 0,
&screensaver_active, 0))
{
std::cerr << "WinAPI failed!" << std::endl;
return ERROR_FAILURE;
}
if (last_input.dwTime < idle_milliseconds && !screensaver_active) {
// user hasn't been idle for long enough
// AND no screensaver is running
Sleep(1000);
continue;
}
// user has been idle at least 10 minutes
do_something();
// done. Wait before doing the next loop.
Sleep(interval);
}
}
注意我在Linux机器上写了这个代码,所以我不能测试它。
Note that I wrote that code on a Linux machine, so I couldn't test it.
这篇关于如果系统处于活动状态,如何检入C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!