Click here to Skip to main content
15,938,218 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

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:
Share this answer
Olivier Levrey 2-May-11 7:54am    
Agree. My 5.
Joan M 2-May-11 9:12am    
Thank you Olivier!
reshmi2000 2-May-11 23:22pm    
I think the multimedia timers use timeSetEvent function. I have done the same way. I also implemented the timer functionality in a worker thread. i cant find a reason why my timer stops after a 2- 3 sec.
Joan M 3-May-11 2:12am    
Check the contents of the timer event procedure. I would try to put only a var=var+1; inside in order to check how many times it is being called.

If there is no stop in calling that and the variable gets increasing without problems, then I would start checking the contents of the timer function.

Probably you call the function before it has stopped again and having different threads (and without knowing a thing about the way you control everything) could lead you to a deadlock state...
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?
Share this answer
reshmi2000 2-May-11 0:40am    
Thanks for the reply. I will explain what i do in OnTimer().

I have upto 20 messages that are actually user inputted data from GUI. All these data are stored in a linked list. For each message, the time duration is also inputted by user. After the linked list for 20 messages is updated, the user presses "Send" button of GUI. Now the timers for each message is started. In the OnTimer() function, the linked list is read and the message based on the "Timer ID"(which is obtained through StartTimer(), and stored as member of each node on linked list) is sent.

This process was taking 63ms for a 50 ms duration. That was why i used timeSetEvent. It was doing the job in correct durations. But getting stopped after certain period of time.

Can you advice me how can i overcome the delay situation in "OnTimer()" or how to use "timeSetEvent" efficiently, with no hanging?
Albert Holguin 2-May-11 0:49am    
Still not completely clear on the user to timing role, but it if I'm interpreting correctly, the user specifies 20 msgs, along with time interval, and hits send? If this is the case, you just need a worker thread to take over after the user hits send (to separate the timer from the main GUI thread and avoid disruptions).
reshmi2000 2-May-11 23:28pm    
i did the code in worker thread , used AfxBeginThread() and did the Start timer and messages sending functionality in the thread. But even now the timer hangs after a predefined time. Do you have any sample of how to do a worker thread?
reshmi2000 3-May-11 7:21am    
The timer is working now. Thanks for the help.
The problem was actually with the hardware, which send the messages.
Anyways, thanks a lot, as i was able to understand the concept of worker thread, and timers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900