Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I know this is straight forward, but somehow I couldn't get google to help me.
I want a timer operation that fires a event every second. I want to do this using threads and waitonsingleobject or something else instead of using system::timers::timer.
can anyone direct me how to do this.
Thanks in advance.
Posted

Not sure why you'd do this but... do something like this (more MFC-like, but I'm sure you can figure an equivalent)...
1) Create Thread
2) Within thread
-while() loop around a sleep/signal loop
-Sleep() for a period of time
-when done with sleep, signal with a PostMessage() to whatever window you want to alert of the timer
3) After execution, stop thread (gracefully!, or else you'll leak any memory there)

Not sure why you'd use WaitForSingleObject() unless you're trying to block the thread that's waiting on the timer... in which case, why have a timer on a different thread?

...Sorry for lack of code, headed to gym! cheers! :)
 
Share this answer
 
May be, you would like to know Waitable Timer Objects[^]. And its APIs for simple usage CreateWaitableTimer()[^], SetWaitableTimer(...)[^], WaitForSingleObject[^], CloseHandle[^].

Edit:
I've forgotten to state that there was Multimedia Timers[^], of course.
 
Share this answer
 
v2
Comments
Albert Holguin 24-Mar-11 21:13pm    
how could you forget mm timers!?! lol... lots of options...
Ozer Karaagac 24-Mar-11 22:10pm    
:-)
Yes, it provides more accuracy and precision.
It was in my mind when I started to write. Forgot it while considering to append to the end.

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