Re: Thread and Timer

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
.



Relevant Pages

  • Re: OO Principle - reference needed
    ... Say an object uses a timer. ... One can pass a Timer reference to a method of SomeObject as a argument. ... In this context, though, the problem is that whoever invokes the SomeObject method must understand the rules and policies of the R1 instantiation _in the course of its primary collaboration with SomeObject_. ... It is the job of the developer to connect the dots of the OO solution in the OOA/D by mapping announcement messages to objects that care what is being announced. ...
    (comp.object)
  • Re: How to discard multiple button clicks?
    ... short period, for instance 1 second, and have the timer reset the variable ... button that calls another subroutine in the click event. ... it executes again when it's done. ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: Getting an out of memory error
    ... > I have a VBV.NET application that runs on a timer to do a job at one hour ... > the code executes an FTP transfer from an FTP site. ... > out of memory error message. ... you could end up running into memory problems. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Timing assembler snippets in x86-64
    ... Starfish wrote: ... I just downloaded Fog's timer and found it to be about as accurate as ... If one executes TSCTest64.exe repeatedly on a Core 2, ...
    (comp.lang.asm.x86)
  • Getting an out of memory error
    ... I have a VBV.NET application that runs on a timer to do a job at one hour ... the code executes an FTP transfer from an FTP site. ... out of memory error message. ... bug in the garbage collector < SORRY, ...
    (microsoft.public.dotnet.languages.vb)