是否可以在WM_TIMER中创建和终止计时器 [英] Is it possible creating and killing timer in WM_TIMER

查看:84
本文介绍了是否可以在WM_TIMER中创建和终止计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在编写MFC C ++应用程序.它包含动画效果(例如移动位图).所以我有一个数组,其中包含每个形状的坐标(使用矩形,椭圆形的"i" m).我的代码很简单
int y [100]; //每种形状的y坐标
然后在OnInitDialog()中,我使用SetTimer(1,100,NULL);并且在OnTimer(UINT nIDEvent)
我的代码是:

for(int j = 1; j< = 100; j ++)
{
if(y [j]> 500)
{
KillTimer(j);
SetTimer(j + 1,100,NULL);
}
}

我的观点是,我要销毁OnTimer(UINT nIDEvent)中的计时器事件,并在同一函数中创建一个新计时器. MFC是否允许该活动,并且这样做是否有任何问题,例如内存泄漏.谢谢!

Hello, i''m writing a MFC C++ app. It contains animation effects(e.g. moving bitmaps). So i have an array that contains the coordinates of each shape(i''m using rectangles, ellipses). My code is simple
int y[100]; //the y-coordinate of each shape
Then in OnInitDialog() i use SetTimer(1, 100, NULL); And in OnTimer(UINT nIDEvent)
my code is:

for(int j=1; j<=100; j++)
{
if(y[j]>500)
{
KillTimer(j);
SetTimer(j+1, 100, NULL);
}
}

My point is that i''m destroying the timer event in OnTimer(UINT nIDEvent) and creating a new timer in the same function. Does MFC allow that activity and are there any problems doing that, such as memory leaks. Thanks!

推荐答案

是的,可以创建一个计时器并在OnTimer中将其终止.这对于仅触发1次的计时器很有用.
Yes, it is possible to create a timer and kill it in OnTimer. This is useful for timers that fire only 1 time.


尽管有可能,但对我来说却毫无意义.
:)
While it is possible, as it stands, it looks pointless to me.
:)


不要!这不是没有意义的...不要听这些家伙...

是的,您可以在OnTimer实现中终止计时器...

当计时器本身想要减慢速度或加快速度时,此功能很有用...

当计时器只想执行固定时间的时间时,例如...:doh:
Nooop! It is not pointless... Don''t listen these guys...

Yes you could kill your timer in the OnTimer implementation...

It can be useful when the timer itself wants to slow down or speedup himself...

It can be useful when a timer want to execute only a fix number of time for example... :doh:


这篇关于是否可以在WM_TIMER中创建和终止计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆