win7设置定时开关机
win7定时开关机
利用休眠来代替关机,然后定时唤醒,再重启
启用休眠
如果执行shutdown -h
提示没有启用休眠功能,则先执行以下命令
powercfg -hibernate on
定时休眠
使用:shutdown -h -f -t 1800
//-t参数对休眠无效,会被任务是错误的命令
正确使用:at 14:00 shutdown -h -f
//14:00强制休眠
定时开机
想使用创建计划任务来唤醒电脑,但是一直不成功,百度的各种方法都试过,后来使用工具WakeupOnStandBy
成功唤醒,wosb.exe 下载地址:https://yunpan.cn/cPtEiiiRVYAaS 访问密码 b08f
wosb.exe /closeall
//清空wosb.exe创建的定时任务
wosb.exe /run /systray /ami tm="15:00" file="shutdown.exe" params="-r -f -t 0"
//15:00唤醒,并且唤醒后重启电脑
注意:如果系统没有启用“允许使用唤醒定时器”功能,唤醒将失败,解决方法可参考我的另一篇文章“电源管理”:http://blog.csdn.net/jin_huan11/article/details/51671556
代码示例
void Util::timerShutdown(EMessageType type, QTime exeTime, bool bImmediate)
{
//时间无效
if(!bImmediate && !exeTime.isValid())
{
g_log->logFunc2(LOG_INFO, "shutdown: %d, time %s is invalid.", type
, TIME_TO_STRING(exeTime).toUtf8().data());
return;
}
//计算exexTime距当前的时间 msc
int msc = 0;
if(!bImmediate)
{
msc = mescToCurrTime(exeTime);
}
QString filePath;
//关机命令
if(type == EMSG_SHUTDOWN)
{
#ifndef WAkEUPONSTANDBY
filePath = "shutdown -s -t " + QString::number(msc / 1000);
#else
//用睡眠来代替关机,这样可以使用wakeuponstandby唤醒电脑
//use sleep replace shutdown
//shutdown -h -t 1800, "-t" is invalid
filePath = "powercfg -hibernate on"; //使用powercfg来启用电脑睡眠功能
Util::startProgram(filePath, false, true);
if(bImmediate)
{
filePath = "shutdown -h -f";
}
else
{
//由于shutdown -h 不能带时间参数, 所以使用at命令操作
filePath = "at /delete /yes"; //清除上一次的at命令
Util::startProgram(filePath, false, true);
filePath = "at " + TIME_TO_STRING(exeTime) + " shutdown -h -f";
}
#endif
}
//开机/唤醒命令
else if(type == EMSG_STANDBY)
{
#ifdef WAkEUPONSTANDBY
filePath = "\"" + qApp->applicationDirPath() + "/wosb.exe\"";
//close all timer
QString commandStr = filePath + " /closeall"; //关闭之前的wosb命令
Util::startProgram(commandStr, false, true);
//stand up
filePath += " /run /systray /ami tm=" + TIME_TO_STRING(exeTime) + " file=\"shutdown.exe\" params=\"-r -f -t 0\"";
#endif
}
//重启命令
else if(type == EMSG_REBOOT)
{
filePath = "shutdown -r -f -t " + QString::number(msc / 1000);
}
bool bShutdown = Util::startProgram(filePath, false, false);
g_log->logFunc2(LOG_INFO, "%s result: %d", filePath.toUtf8().data(), bShutdown);
}