定时关机软件该怎么做

pyl2001 2004-01-05 09:11:31
保存设置时间,设置定时器不断取当前时间和设置的时间比较?这样似乎有问题,当系统繁忙的时候,没时间执行定时关机程序,当定时关机程序获得执行cpu时间片时,时间已经过了设置时间,这时候理应执行关机,但如果采用时间比较,是不是就做不到了,一般应该用怎样的判断逻辑?
...全文
189 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
greensofter 2004-01-15
  • 打赏
  • 举报
回复
哪就currenttime >= settingtime :)
pyl2001 2004-01-15
  • 打赏
  • 举报
回复
关机已经做了,定时也做了,只是比较时间,我觉得这样不好
shootingstars 2004-01-15
  • 打赏
  • 举报
回复
SetWaitableTimer
The SetWaitableTimer function activates the specified waitable timer. When the due time arrives, the timer is signaled and the thread that set the timer calls the optional completion routine.

BOOL SetWaitableTimer(
HANDLE hTimer, // handle to timer
const LARGE_INTEGER *pDueTime, // timer due time
LONG lPeriod, // timer interval
PTIMERAPCROUTINE pfnCompletionRoutine, // completion routine
LPVOID lpArgToCompletionRoutine, // completion routine parameter
BOOL fResume // resume state
);

其中第二个参数可以设置第一次报时的时间,注意它使用的是UTC时间。
具体参看windows核心编程第9章
findingit 2004-01-15
  • 打赏
  • 举报
回复
time_t star=now();
long timeout=60*60;//1小时

onTimer()
{
time_t end=now();
if(end - star >=timeout)
{
//关机代码...
}
}
zhangcrony 2004-01-14
  • 打赏
  • 举报
回复
//先告诉你怎么关机:
DWORD dwVersion;
TOKEN_PRIVILEGES tkp;
HANDLE hToken;

dwVersion = GetVersion();
if(dwVersion < 0x80000000)
{//NT

if (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
{
MessageBox("OpenProcessToken failed!");
}


LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid); //获得本地机唯一的标识
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES) NULL, 0); //调整获得的权限

if (GetLastError() != ERROR_SUCCESS)
{
MessageBox("AdjustTokenPrivileges enable failed!");
}

BOOL fResult =InitiateSystemShutdown(
"Lf",
"WINDOWS将在上面的时间内关机,请做好保存工作!", // 显示的消息
10, // 关机所需的时间
TRUE,
TRUE); //设为TRUE为重起,设为FALSE为关机

if(!fResult)
{
MessageBox("InitiateSystemShutdown failed.");
}
}

else
{//95
ExitWindowsEx(EWX_FORCE | EWX_SHUTDOWN,0);
}
//要定时关机只要设个Timer就行了
theone 2004-01-07
  • 打赏
  • 举报
回复
但如果采用时间比较,是不是就做不到了
~~~~~~~~~~~~~~~~~~··
为什么你会认为做不到呢?
wdllove 2004-01-07
  • 打赏
  • 举报
回复
做个关机的部分,然后在"计划任务"里设置一下!
呵呵!简单吧
jinn 2004-01-07
  • 打赏
  • 举报
回复
你可以判断是否大于等于你所设置的时间,就算过了一点也一样可以执行关机程序。
pyl2001 2004-01-07
  • 打赏
  • 举报
回复
to theone
假设设定的时间是2004-01-01-14:00
如果到这个时间的时候由于系统正在忙于处理优先级非常高的任务,没时间执行我的程序
等到14:01才有时间来执行,假设我的判断是if currenttime == settingtime then shutdown
那不是执行不到了么。

2,643

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 硬件/系统
社区管理员
  • 硬件/系统社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧