关于定时器的实现

lzxjxsy 2006-12-01 06:54:13
各位大哥,关于定时器的实现和应用,请大家帮我看看,下面两段代码有什么不同,为什么一个能实现定时功能,一个不能实现.

不能实现的如下:
#include <windows.h>
#include <process.h>
#include <stdio.h>
VOID CALLBACK TimerHandler( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
printf("ok!\n");
}

class clsTimer{
public:
static int setOneTimer(int nInterval, TIMERPROC taskFun);
void start();

static void gTaskThreadFunc(LPVOID pParam);
};

void clsTimer::start()
{

Sleep(1000);
_beginthread(gTaskThreadFunc, 0, NULL);
}

void clsTimer::gTaskThreadFunc(LPVOID pParam)
{
// setOneTimer(1000, (TIMERPROC)TimerHandler);
while(true)
{
MSG message;
Sleep(1);
while (::PeekMessage(&message, NULL, 0, 0, PM_REMOVE))
{
::TranslateMessage(&message);
::DispatchMessage(&message);
}
}
}

int clsTimer::setOneTimer(int nInterval, TIMERPROC taskFun)
{
return SetTimer(NULL, 0, nInterval, taskFun);
}

int main()
{
clsTimer ct;
ct.setOneTimer(1000, (TIMERPROC)TimerHandler);
ct.start();

while(1)
{
Sleep(1000);
}
return 0;
}


能实现的如下:
#include <windows.h>
#include <process.h>
#include <stdio.h>
VOID CALLBACK TimerHandler( HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
printf("ok!\n");
}

class clsTimer{
public:
static int setOneTimer(int nInterval, TIMERPROC taskFun);
void start();

static void gTaskThreadFunc(LPVOID pParam);
};

void clsTimer::start()
{

Sleep(1000);
_beginthread(gTaskThreadFunc, 0, NULL);
}

void clsTimer::gTaskThreadFunc(LPVOID pParam)
{
setOneTimer(1000, (TIMERPROC)TimerHandler);
while(true)
{
MSG message;
Sleep(1);
while (::PeekMessage(&message, NULL, 0, 0, PM_REMOVE))
{
::TranslateMessage(&message);
::DispatchMessage(&message);
}
}
}

int clsTimer::setOneTimer(int nInterval, TIMERPROC taskFun)
{
return SetTimer(NULL, 0, nInterval, taskFun);
}

int main()
{
clsTimer ct;
// ct.setOneTimer(1000, (TIMERPROC)TimerHandler);
ct.start();

while(1)
{
Sleep(1000);
}
return 0;
}

拜托大家了,如果解答得好,兄弟再开贴给分,绝不食言。两段代码的区别是setOneTimer(1000, (TIMERPROC)TimerHandler); 的位置不同

...全文
166 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuqiyc 2006-12-05
  • 打赏
  • 举报
回复
If hWnd is NULL, PeekMessage retrieves messages for any window that belongs to the current thread. If hWnd is INVALID_HANDLE_VALUE, PeekMessage retrieves messages whose hWnd value is NULL, as posted by the PostThreadMessage function.
----------------------------------------
只说是当前线程(current thread),没有说是整个应用程序。不知道你在哪儿看的?
lzxjxsy 2006-12-05
  • 打赏
  • 举报
回复
我就是想问为什么在不同的线程里面他们不能响应,应为根据msdn上的说明,应该是可以的,应为PeekMessage第一个参数为null,应该接受该应用程序的所有消息,而且SetTimer的参数还可以为空,这时候不指定任何form.
Stefine 2006-12-01
  • 打赏
  • 举报
回复
同意一楼

An application can process WM_TIMER messages by including a WM_TIMER case statement in the window procedure or by specifying a TimerProc callback function when creating the timer. When you specify a TimerProc callback function, the default window procedure calls the callback function when it processes WM_TIMER. Therefore, you need to dispatch messages in the calling thread, even when you use TimerProc instead of processing WM_TIMER.

PS: 请KillTimer
一条晚起的虫 2006-12-01
  • 打赏
  • 举报
回复
msdn
lzxjxsy 2006-12-01
  • 打赏
  • 举报
回复
可否提供一下更加详细的资料啊,拜托了。你这句话是从哪里摘来的啊,谢谢了。
hxzmm 2006-12-01
  • 打赏
  • 举报
回复
This window must be owned by the calling thread.
这是SetTimer的hWnd参数的解释!!只能在同一个线程当中!!

16,472

社区成员

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

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

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