Simple question - Threads and Timers
-
I have a worker thread and need to start and stop a timer. As I understand there are two ways to do this, using CWnd class and handling OnTimer()or SetTimer(hWnd, ....) API call, but both rely on having a UI to receive WM_TIMER messages. Is this correct? :confused: If yes what is the best way to implement a timer in a worker thread, that does not interact with the UI at all?
-
I have a worker thread and need to start and stop a timer. As I understand there are two ways to do this, using CWnd class and handling OnTimer()or SetTimer(hWnd, ....) API call, but both rely on having a UI to receive WM_TIMER messages. Is this correct? :confused: If yes what is the best way to implement a timer in a worker thread, that does not interact with the UI at all?
no_reg_name wrote: sing CWnd class and handling OnTimer()or SetTimer(hWnd, ....) API call, but both rely on having a UI to receive WM_TIMER messages. Is this correct? Very Much Correct ,No problem using timer in UI no_reg_name wrote: what is the best way to implement a timer in a worker thread, that does not interact with the UI at all? You have to use
Sleep
api to simulate the Timer in worker thread.
"I Think this Will Help"
visit me at http://www.thisisalok.tk
-
I have a worker thread and need to start and stop a timer. As I understand there are two ways to do this, using CWnd class and handling OnTimer()or SetTimer(hWnd, ....) API call, but both rely on having a UI to receive WM_TIMER messages. Is this correct? :confused: If yes what is the best way to implement a timer in a worker thread, that does not interact with the UI at all?
Look at CreateWaitableTimer and use WaitForSingleObject or else WaitForMultipleObjects, if you have another event or such to signal your thread to exit or to perform some other work. Instead of using Sleep() os LeepEx() which will lock up your thread until the sleep has timed out.
-
I have a worker thread and need to start and stop a timer. As I understand there are two ways to do this, using CWnd class and handling OnTimer()or SetTimer(hWnd, ....) API call, but both rely on having a UI to receive WM_TIMER messages. Is this correct? :confused: If yes what is the best way to implement a timer in a worker thread, that does not interact with the UI at all?
There are two ways to use the SetTimer API. The first is as you said where you specify an HWND that will recieve the WM_TIMER message. The second is to use the CALLBACK function that you specify in the fourth parameter of the SetTimer() function. See SetTimer[^] and TimerProc[^] in MSDN for more info.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!
-
I have a worker thread and need to start and stop a timer. As I understand there are two ways to do this, using CWnd class and handling OnTimer()or SetTimer(hWnd, ....) API call, but both rely on having a UI to receive WM_TIMER messages. Is this correct? :confused: If yes what is the best way to implement a timer in a worker thread, that does not interact with the UI at all?
You do not have to have a window to create a timer. You can create a timer if you supply NULL for the hWnd and supply a callback proc. onwards and upwards...
-
There are two ways to use the SetTimer API. The first is as you said where you specify an HWND that will recieve the WM_TIMER message. The second is to use the CALLBACK function that you specify in the fourth parameter of the SetTimer() function. See SetTimer[^] and TimerProc[^] in MSDN for more info.
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!
I guess you can type faster than me....:laugh: Beat me by two seconds... onwards and upwards...
-
I guess you can type faster than me....:laugh: Beat me by two seconds... onwards and upwards...
basementman wrote: I guess you can type faster than me Yeah, my two fingers do move pretty fast:~
"You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ??? You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!