定时器

lodemenwwwww 2010-08-05 12:47:00
除了用settime 以外
还可以怎么实现定时器
我现在是要求在一个星期内七天每天不同的时间做不同的事情
用settime可以实现
但是自我感觉效果不好
还有没有别的方法
...全文
132 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
汪宁宇 2010-08-07
  • 打赏
  • 举报
回复
也可以用线程实现定时器,这现还可以避免UI挂起....

#define TIMER_SLEEP     10000

DWORD WINAPI TimerThread(LPARAM pamaram)
{
UINT oldTickCount, newTickCount;
oldTickCount = GetTickCount();
while(TRUE)
{
while(TRUE)
{
newTickCount = GetTickCount();
if(newTickCount - oldTickCount >= TIMER_SLEEP)
{
oldTickCount = newTickCount;
break;
}
else
SwitchToThread();
}
// Call your function
}
return 0;
}

HANDLE hThread = CreateThread....
phisherr 2010-08-07
  • 打赏
  • 举报
回复
定时器里面调用GetSystemTime,然后在根据时间决定该干什么
lodemenwwwww 2010-08-06
  • 打赏
  • 举报
回复
我现在用的就是GetSystemTime
不过大家的意见我会先试验一下
有结果了我给大家分
wltg2001 2010-08-05
  • 打赏
  • 举报
回复
另外还有一个多媒体定时器,你参考一下这个:参考1
参考2
wltg2001 2010-08-05
  • 打赏
  • 举报
回复
可以用CreateWaitableTimer创建一个等待定时器内核对象来达到定时的效果
MSDN有个例子:
Using Waitable Timer Objects
The following example creates a timer that will be signaled after a 10 second delay. First, the code uses the CreateWaitableTimer function to create a waitable timer object. Then it uses the SetWaitableTimer function to set the timer. The code uses the WaitForSingleObject function to determine when the timer has been signaled.



#define _WIN32_WINNT 0x0500

#include <windows.h>
#include <stdio.h>

int main()
{
HANDLE hTimer = NULL;
LARGE_INTEGER liDueTime;

liDueTime.QuadPart=-100000000;

// Create a waitable timer.
hTimer = CreateWaitableTimer(NULL, TRUE, "WaitableTimer");
if (!hTimer)
{
printf("CreateWaitableTimer failed (%d)\n", GetLastError());
return 1;
}

printf("Waiting for 10 seconds...\n");

// Set a timer to wait for 10 seconds.
if (!SetWaitableTimer(hTimer, &liDueTime, 0, NULL, NULL, 0))
{
printf("SetWaitableTimer failed (%d)\n", GetLastError());
return 2;
}

// Wait for the timer.

if (WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0)
printf("WaitForSingleObject failed (%d)\n", GetLastError());
else printf("Timer was signaled.\n");

return 0;
}
freelaxy 2010-08-05
  • 打赏
  • 举报
回复
方法多了去了, 动动脑子, 用GetSystemTime总可以吧.
pjl110 2010-08-05
  • 打赏
  • 举报
回复
如果是隔了天的话,应该是要考虑关机的情况的,而不是考虑定时器的问题。

是不是应该让程序自启动,然后监视计算机时间然后根据时间来判断该做什么!

GetSystemTime不错!
freelaxy 2010-08-05
  • 打赏
  • 举报
回复
用SetTimer或其它的定时器, 你一周七天不关机? 断电怎么办? Windows DOWN怎么半?
想想Windows Schedule怎么做...

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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