From: lizhi on
Hi all,
My case is like this:
I have two threads, which are both CWinThread object, one of them is main
thread.
When that non-main thread starts, it will enter the thread work
function--_AfxThreadEntry, which is one private method MFC provides. During
this method is called, m_pMainWnd, the member of this thread, will be
attached to the main window of my application.
My question is:
If the two message loops are both running, which thread will get the message
sent to the main window?
If the message loop of one thread is ahead of another, what is the result
again?

Thanks a lot.

Robert


From: Scott McPhillips [MVP] on
Each message goes to one thread only. If a window is created in a thread,
all messages for that window go to the thread in which the window was
created.

"lizhi" <zhi_lee(a)163.com> wrote in message
news:etACbOMtIHA.4528(a)TK2MSFTNGP03.phx.gbl...
> Hi all,
> My case is like this:
> I have two threads, which are both CWinThread object, one of them is main
> thread.
> When that non-main thread starts, it will enter the thread work
> function--_AfxThreadEntry, which is one private method MFC provides.
> During this method is called, m_pMainWnd, the member of this thread, will
> be attached to the main window of my application.
> My question is:
> If the two message loops are both running, which thread will get the
> message sent to the main window?
> If the message loop of one thread is ahead of another, what is the result
> again?
>
> Thanks a lot.
>
> Robert
>
>

--
Scott McPhillips [VC++ MVP]

From: Joseph M. Newcomer on
First, there is a crucial question: why do you think a message should go to a secondary
thread? Does this thread have windows of its own? Are they user-visible?

A secondary thread should not own or attempt to use any user-visible window at any time.
If you have a secondary thread creating user-visible windows, you probably have so many
things wrong in your code that you are going to get into serious trouble not very far down
the road. If you have done this, rethink your design before it is too late.

Messages to a window are handled by the thread that owns the window. There is no possible
concept of a message loop of one thread being "ahead" of another; the message loops are
unrelated. The thread that owns the window works in total ignorance of any other possible
message loop that exists, and consequently the existence of multiple message pumps is
irrelevant to your main GUI thread's message pump. It neither knows nor cares about them.

A secondary thread may or may not have a message pump. Secondary threads with message
pumps are good for lots of things, but owning user-visible windows is not among them.

Note, by the way, that the ONLY thing that determines what message pump receives a message
is what windows that thread owns. If, for example, you were to assign the m_pMainWnd
window to a secondary thread, this would have absolutely no effect on which thread
received messages; think of the m_pMainWnd as being a convenient annotation. The window
referenced does not affect how message routing is done in the kernel, because Windows
itself has no idea that MFC exists, and details like m_pMainWnd are irrelevant to it. It
sends themessage to the thread that owns the window, and does not care about random
variables that the application might be using to track those windows. Thus assigning
m_pMainWnd to another thread will have no effect on the kernel, but MFC itself will start
taking assertion failures (and perhaps crashing on access faults or something else
seriously unpleasant) because you would have violated fundamental assumptions about its
behavior.
joe

On Tue, 13 May 2008 14:39:28 +0800, "lizhi" <zhi_lee(a)163.com> wrote:

>Hi all,
>My case is like this:
>I have two threads, which are both CWinThread object, one of them is main
>thread.
>When that non-main thread starts, it will enter the thread work
>function--_AfxThreadEntry, which is one private method MFC provides. During
>this method is called, m_pMainWnd, the member of this thread, will be
>attached to the main window of my application.
>My question is:
>If the two message loops are both running, which thread will get the message
>sent to the main window?
>If the message loop of one thread is ahead of another, what is the result
>again?
>
>Thanks a lot.
>
>Robert
>
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm