您的位置:程序门 -> c/c++ -> c++ 语言



怎么样在标准c++中实现定时器的使用?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


怎么样在标准c++中实现定时器的使用?
发表于:2007-07-24 18:26:43 楼主
在第一个程序中我调用了settimer这个函数,设想是每过5秒钟到了之后执行timerproc函数,但是函数timerproc还没有执行,整个程序就结束了,没有结果输出。
#include "windows.h "
#include "iostream.h "

void   callback   timerproc(hwnd   hwnd,uint   umsg,uint   idevent,dword   dwtime);

void   main()
{
      settimer(null,1,5000,timerproc);
}


void   callback   timerproc(hwnd   hwnd,uint   umsg,uint   idevent,dword   dwtime)
{
      cout < < "hello " < <endl;
}

在这个程序中,我只是在settimer的后面加上一个死循环,目的就是为了不是函数过早地结束,但是程序一直运行在死循环这,没有我想要的每隔5秒钟发一个hello!.
#include "windows.h "
#include "iostream.h "

void   callback   timerproc(hwnd   hwnd,uint   umsg,uint   idevent,dword   dwtime);

void   main()
{
      settimer(null,1,5000,timerproc);
    while(1)
      {};
     
}


void   callback   timerproc(hwnd   hwnd,uint   umsg,uint   idevent,dword   dwtime)
{
      cout < < "hello! " < <endl;
}

那位高手能帮我解答,十分感谢
发表于:2007-07-24 20:48:141楼 得分:0
when   you   specify   a   timerproc   callback   function,   the   default   window   procedure   calls   the   callback   function   when   it   processes   wm_timer

你的程序中没有调用defaultwindowproc,所以timerproc不会被调用
发表于:2007-07-24 22:29:172楼 得分:0
标准c++是什么意思?只用标准库?但是你的程序中又调用了win32   api
发表于:2007-07-25 08:24:403楼 得分:0
[ref]

1.1   用wm_timer来设置定时器

先请看settimer这个api函数的原型

uint_ptr   settimer(
hwnd   hwnd,   //   窗口句柄
uint_ptr   nidevent,   //   定时器id,多个定时器时,可以通过该id判断是哪个定时器
uint   uelapse,   //   时间间隔,单位为毫秒
timerproc   lptimerfunc   //   回调函数
);

例如
settimer(m_hwnd,1,1000,null);   //一个1秒触发一次的定时器
在mfc程序中settimer被封装在cwnd类中,调用就不用指定窗口句柄了,例如:

uint   settimer(1,100,null);
函数反回值就是第一个参数值1,表示此定时器的id号。

第二个参数表示要等待100毫秒时间再重新处理一次。第三个参数在这种方法中一般用null。
注意:设置第二个参数时要注意,如果设置的等待时间比处理时间短,程序就会出问题了。

1.2   调用回调函数

此方法首先写一个如下格式的回调函数

void   callback   timerproc(hwnd   hwnd,uint   nmsg,uint   ntimerid,dword   dwtime);
然后再用settimer(1,100,timerproc)函数来建一个定时器,第三个参数就是回调函数地址。

二、多个定时器的实现与应用


我们在安装定时器时都为其指定了id,使用多个定时器时,该id就发挥作用了。
不使用mfc时,当接收到wm_timer消息,wparam   wparam中的值便是该定时器的id
使用mfc时就更简单了,我们为其增加wm_time的消息处理函数ontimer即可,请看如下例子
void   ctimertestdlg::ontimer(uint   nidevent)
{
switch   (nidevent)
{
case   24:   ///处理id为24的定时器
draw1();
break;
case   25:   ///处理id为25的定时器
draw2();
break;
}
cdialog::ontimer(nidevent);
}
当你用回调函数时,我们可以根据ntimerid的值来判断是哪个定时器,例如:
void   callback   timerproc(hwnd   hwnd,uint   nmsg,uint   ntimerid,dword   dwtime)
{
switch(ntimerid)
{
case   1:   ///处理id为1的定时器
do1();
break;
case   2:   ///处理id为2的定时器
do2();
break;
}
}
三、取消定时器

不再使用定时器后,我们应该调用killtimer来取消定时,killtimer的原型如下

bool   killtimer(
hwnd   hwnd,   //   窗口句柄
uint_ptr   uidevent   //   id
);
在mfc程序中我们可以直接调用killtimer(int   nidevent)来取消定时器。
发表于:2007-07-25 08:33:034楼 得分:0
我不知道没有消息处理的c++能支持回调哈。
发表于:2007-07-25 09:45:345楼 得分:0
劝lz先搞明白   标准c++   与   vc   的区别
发表于:2007-07-25 15:49:296楼 得分:0
你这个程序需要添加一段对windows的消息进行处理的程序即可。
具体如下:
#include   <windows.h>
#include   <iostream>

void   callback   timerproc(hwnd   hwnd,uint   umsg,uint   idevent,dword   dwtime);

void   callback   timerproc(hwnd   hwnd,uint   umsg,uint   idevent,dword   dwtime)
{
std::cout   < <   "hello "   < <   std::endl;
}

void   main()
{
int   timer1   =   1;
hwnd   hwndtimer;      
msg   msg;                    

settimer(null,timer1,5000,timerproc);
while   (getmessage(&msg,    
null,                      
null,                      
null)                    
!=   0   &&   getmessage(&msg,   null,   null,   null)   !=   -1)
{  
if   (msg.message   ==   wm_timer)  
{  
std::cout   < <   "i   got   the   message "   < <   std::endl;
translatemessage(&msg);  
dispatchmessage(&msg);    
}  
}  
}

输出如下:
i   got   the   message
hello
i   got   the   message
hello
i   got   the   message
hello

发表于:2007-07-26 09:17:247楼 得分:0
对while部分进行修改如下:
int   itemp;
while   (   (itemp   =   getmessage(&msg,   null,null,null))&&   (itemp!=0)   &&   (-1   !=   itemp))
发表于:2007-07-26 10:44:318楼 得分:0
你调用了#include "windows.h ",还说是标准c++
标准c++库没有提供定时器,线程,网络等与操作系统有关的东西.
发表于:2007-07-26 11:40:379楼 得分:0
记得好像boost有这个功能
发表于:2007-07-26 12:16:1310楼 得分:0
c++中的定时器都要使用到第三方的东西。   要么自己写线程来做定时器,虽然精度差点,不过一般都可以使用,要求不会精确到毫秒级。标准c++没有提供这个功能。
发表于:2007-07-26 12:23:0011楼 得分:0
settimer   is   not   a   function   belong   standard   c++.  

settimer   process   wm_timer   message   for   you   application.   you   need   drive   windows   message   queue   to   run.
发表于:2007-07-26 12:31:3612楼 得分:0
只用标准c++恐怕实现不了吧。但你可以使用boost.timer。
发表于:2007-07-26 13:05:2313楼 得分:0
帮顶,不过   很讨厌api,555
发表于:2007-07-26 18:05:0114楼 得分:0
用gettickcount+线程实现把
发表于:2007-07-26 18:14:2915楼 得分:0
windows下的东西
发表于:2007-07-26 21:09:3716楼 得分:0
用gettickcount+线程实现把
---------------------------
貌似不必要了吧.    

lz这段代码,   就不需要什么线程了.   一个双层的while搞定.  
while(true)
{
int   ncur
发表于:2007-07-26 21:11:2717楼 得分:0

while(true)
{
int   ncurtime   =   gettickcount();
while(gettickcount()-ncurtime   <   5000)
sleep(500);
cout < < "hello! " < <endl;
}



快速检索

最新资讯
热门点击