Re: Thread and Timer
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Sat, 15 Mar 2008 13:35:51 -0500
The timer is associated with the thread that calls the SetTimer API. It does not care in
the slightest how these instructions have been wrapped by C++ classes, or the class names,
or anything else. So if you have a CWinThread class, it has code, and data. That's all
it has: code and data. The code is executed in the context of the thread that calls the
code. If you do AfxBeginThread, the code of the target function runs in the context of
the newly-created thread, and everything that code calls executes in the context of that
thread. If you do ANOTHER AfxBeginThread, the code of the target function runs in the
context of this second thread, and everything that code calls executes in the context of
that second thread. If you call a function from your main thread, the MFC class and the
name of the MFC class have NOTHING to do with the context in which the code is executed;
the code is executed in the context of the thread that calls it, and that is your main GUI
thread. Therefore, your timer messages will be sent to that thread, because that's what
you asked it to do.
Do not confuse superficial syntax with what is really going on. You can CALL it a "thread
class", DERIVE it from CWinThread, but the code is just code, and it always executes in
the context of the calling thread. No exception (note that cross-thread SendMessage does
not change this; what happens is that the sending thread is suspended, the thread which
receives the message is activated, and the code is executed in the context of the
receiving thread. SendMessage is *not* a "call", it is a request to deschedule one
thread and reschedule another. Thus the code is always executed in the context of the
calling thread, because the OS itself initiates the call of the receiving thread in the
context of the receiving thread.
Of course you can define a call within the thread. It can happen in the top-level thread
function, for example, or in a UI thread, in the InitInstance for that thread. Then the
code is called from the thread, and will impact the thread that called it.
On Sat, 15 Mar 2008 04:24:10 -0700 (PDT), Stefano <posting@xxxxxxxxxx> wrote:
On 14 Mar, 17:36, "AliR \(VC++ MVP\)" <A...@xxxxxxxxxxxxx> wrote:****
If you want to set a timer in the thread from a dialog in your main thread,
then you will have to post a user defined message to the thread to tell it
to set the timer. You can't just call a method in your thread. As Scott
has already pointed out it will be setting the timer for the calling thread
(which is the main thread)
I call the CMyThread::Monitor method from my dialog. Does this mean
than the timer will be associate to the main dialog ?
Can I define a method, inside the thread, to start the timer ?
void CMyThread::Monitor(...)
{
m_TimerID = SetTimer(NULL,0,5000,NULL);
}
void CMyDialog::OnEvent()
{
m_pThread->Monitor();
}
Yes, this is incorrect, for the reasons already stated. If the secondary thread is a UI
thread, the OnEvent code could PostThreadMessage to the UI secondary thread a notification
that causes the receiving thread to SetTimer, but this can only work if there is actually
an active message pump in the secondary thread. If it is a pure-computation thread in an
infinite loop, you have to be able to set a flag that says "please set the timer and clear
this flag", and this flag must be tested with reasonable frequency to be effective.
joe
*****
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- Follow-Ups:
- Re: Thread and Timer
- From: Stefano
- Re: Thread and Timer
- References:
- Re: Thread and Timer
- From: Joseph M . Newcomer
- Re: Thread and Timer
- From: Stefano
- Re: Thread and Timer
- From: Joseph M . Newcomer
- Re: Thread and Timer
- From: AliR \(VC++ MVP\)
- Re: Thread and Timer
- From: Joseph M . Newcomer
- Re: Thread and Timer
- From: AliR \(VC++ MVP\)
- Re: Thread and Timer
- From: Stefano
- Re: Thread and Timer
- From: AliR \(VC++ MVP\)
- Re: Thread and Timer
- From: Stefano
- Re: Thread and Timer
- Prev by Date: Re: CWnd to Full Screen
- Next by Date: Re: UNICODE conversion
- Previous by thread: Re: Thread and Timer
- Next by thread: Re: Thread and Timer
- Index(es):
Relevant Pages
|