MFC中的准确工作计时器 [英] Accurate working timer in MFC

查看:101
本文介绍了MFC中的准确工作计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个分析器,它通过网络连续发送消息(数据包).
例如:-2条消息-每50毫秒发送一条消息1;每100毫秒发送一次Message2.实际上,用户是通过编辑框输入50毫秒和100毫秒.
对于计时器,我首先实现了OnTimer(),SetTimer和KillTimer函数.但是此计时器的准确性不高,因为发送消息的持续时间低至50毫秒.
因此,我现在使用了一个回调函数,并使用了"timeSetEvent".这次,在要求的时间延迟内准确地发送了消息.但是,经过一段时间的工作后,计时器会挂在回调函数内部.

是因为我没有在单独的线程中实现计时器吗?如何实现"timSetEvent",以便连续工作?
请帮助我.

Hi, I am doing a Analyser which sends messages(packets) over a network continously.
Eg:- 2 messages - Message1 at every 50 ms; and Message2 at every 100 ms. The 50ms and 100 ms is actually input from the user through an edit box.
For the timer, i first implemented OnTimer(), SetTimer and KillTimer functions. But this timer was not so accurate as the time duration for sending message becomes as low as 50ms.
So I used now a callback function, and used "timeSetEvent". This time the messages were sent accurately at the required time delays. But after some time of working, the timer hangs inside the call back function.

Is it because i have not implemented the timer in a seperate thread? How can i implement the "timSetEvent", so as to work continously?
Please help me.

推荐答案

看看以下链接: ^ ].

您将在那里获得更多信息,但是WM_TIMER消息的工作方式当然使其成为不太重要的计时器事件的理想选择,但是如果您需要关键的响应,则这些消息将更好地工作...
HTH! :thumbsup:
Take a look at this link: Multimedia Timers[^].

You will be able to get more information there, but of course the way the WM_TIMER message works make it ideal for not so critical timer events, but if you need critical responses those ones will work better...

HTH! :thumbsup:


OnTimer()应该在这些时间间隔(不是那么小的间隔,SetTimer支持低至10ms)上是准确的,但是您必须记住作为OnTimer()进行操作,计时器将作为消息被回调,因此,如果在同一线程中有很多事情在进行,则它可能会延迟消息的处理(因为它与任何其他Windows消息一起排队).

我认为通过良好的实现,您应该能够使用OnTimer,SetTimer和KillTimer:
-SetTimer和KillTimer在执行期间仅应调用一次(这是您所有应用程序需要的)
-两个间隔相互成倍,您只需一个计时器
-如果确实需要这种准确性,则处理定时事件的方法应在该线程中尽可能少地执行,其他任何工作都应移至另一个线程.

就挂起timeSetEvent()而言,这几乎表明您没有以非常好的方式做某事,如果我不得不猜测,您是直接从该对象以很高的速率(绝对不是个好主意)访问GUI对象.回调.

与此相关的是,如何在该时间间隔内进行用户输入?
The OnTimer() should be accurate at those time intervals (that''s not that small of interval, SetTimer supports down to 10ms) but you have to remember that when you do it as OnTimer(), the timer is called back as a message, therefore if you have a lot of things going on in that same thread, it may delay the processing of the message (since its queued with any other windows message).

I think with good implementation, you should be able to use OnTimer, SetTimer, and KillTimer:
-SetTimer and KillTimer should only be called once during execution (that''s all your application needs)
-With your two intervals being multiple of each other, you only need one timer
-If you really need that accuracy, your method that handles the timed event should do as little as possible in that thread, any other work should go off to another thread.

As far as timeSetEvent() hanging, that almost shows that you''re not doing something in a very good manner, if I had to guess, you''re accessing GUI objects at that high rate (never a good idea) directly from the callback.

On a related note, how can user input occur at that interval?


这篇关于MFC中的准确工作计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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