标题:【求助】c语言定时器为何不好用
只看楼主
ccwoss
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-1-27
结帖率:0
已结贴  问题点数:20 回复次数:5 
【求助】c语言定时器为何不好用
是VC的console环境,准备创建个定时器,代码如下,编译运行OK,确看不到dbg1被调用:
long g_dbg1 = 0 ;
void CALLBACK dbg1(HWND hWnd,UINT nMsg,UINT nTimerid,DWORD dwTime)
{
    g_dbg1++;
    //printf("dbg1-%u\r\n", g_dbg1);
}

long dbg2()
{
        return (long)SetTimer(0, 0,1000, dbg1);
}
搜索更多相关主题的帖子: long 定时器 return 
2013-01-28 10:39
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6809
专家分:42393
注 册:2010-12-16
得分:7 


hWnd
[in] Handle to the window to be associated with the timer. This window must be owned by the calling thread. If this parameter is NULL, no window is associated with the timer and the nIDEvent parameter is ignored.
nIDEvent
[in] Specifies a nonzero timer identifier. If the hWnd parameter is NULL, this parameter is ignored.
uElapse
[in] Specifies the time-out value, in milliseconds.
lpTimerFunc
[in] Long pointer to the function to be notified when the time-out value elapses. For more information about the function, see TimerProc.

If lpTimerFunc is NULL, the system posts a WM_TIMER message to the application queue. The hwnd member of the message's MSG structure contains the value of the hWnd parameter.



Return Value

--------------------------------------------------------------------------------



An integer identifying the new timer indicates success. An application can pass this value, or the string identifier, if it exists, to the KillTimer function to destroy the timer. Zero indicates failure. To get extended error information, call GetLastError.



Remarks

--------------------------------------------------------------------------------



If the uElapse parameter is set to INFINITE, KillTimer fails to destroy the timer.

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.

The wParam parameter of the WM_TIMER message contains the value of the nIDEvent parameter.

If SetTimer is called with hWnd set to NULL and lpTimerFunc set to NULL, WM_TIMER message will not be delivered.


自己翻译下吧。不难,你的问题估计在里面也能找到

[ 本帖最后由 yuccn 于 2013-1-28 10:53 编辑 ]

我行我乐
我的博客:
http://blog.yuccn. net
2013-01-28 10:50
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
得分:7 
得有消息循环才能用这个罢

还是那个话 无代码 无真相
2013-01-28 11:12
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:7 
以下是引用zklhp在2013-1-28 11:12:22的发言:

得有消息循环才能用这个罢
 
还是那个话 无代码 无真相
up

所以可以用 多媒体定时器 timeSetEvent
2013-01-28 12:27
ccwoss
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-1-27
得分:0 
我这个是console程序,所以hWnd
是0,nIDEvent没有意义,也没有消息通知。应该直接调用回调函数呀,却没有看到调用,不明白?
2013-01-28 16:31
i602313
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2012-8-13
得分:0 
你的问题解决了吗?我也遇到定时器的问题了,我的那个定时器工作一段时间后就不工作了,好像就进不了中断了,不知问题处在哪里。
2013-04-01 14:59



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-397780-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.263126 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved