Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SendMessage vs PostMessage

541 views
Skip to first unread message

Max Schölle

unread,
Dec 4, 2001, 5:30:57 AM12/4/01
to
I derived a Dialog-Class from CDialog. In OnInitDialog I start a worker
thread which sends WM_APP+1 upon completion of its work. I want to catch
this message in the PreTranslateMessage-Handler of my dialog. But
SendMessage() returns without calling PreTranslateMessage! Using
PostMessage() works like I expexted SendMessage() to work. What's wrong ??

SendMessage is to wait until the message was accepted, PostMessage just
queues the message into the message-queue?


Ben Newsam

unread,
Dec 4, 2001, 6:00:05 AM12/4/01
to
In article <9ui8rm$30e9$1...@wrath.news.nacamar.de>, Max Schölle
<Ma...@t-online.de> writes

>SendMessage is to wait until the message was accepted, PostMessage just
>queues the message into the message-queue?

As I understand it, SendMessage calls the window procedure directly.
--
Ben Newsam

TiTi

unread,
Dec 4, 2001, 7:41:45 AM12/4/01
to
"Max Schölle" <Ma...@t-online.de> wrote in message
news:9ui8rm$30e9$1...@wrath.news.nacamar.de...


SendMessage waits until the message was processed, that's a different thing.
PostMessage send the message, places it in a queue. This message will be
processed when the time is right.

Ciao
--
TiTi (Tom Tempelaere)
Upsilon S.A. - Financial and Computer Engineering
Luxembourg
"All your base are belong to us!" - Cats/Zero Wing


Abbas Cakmak

unread,
Dec 4, 2001, 9:15:11 AM12/4/01
to

Max Schölle schrieb in Nachricht <9ui8rm$30e9$1...@wrath.news.nacamar.de>...

Why do you want to process the message in PreTranslateMessage? Just put a
message handler to your Dialog class with the ON_MESSAGE macro.

Cheers

Abbas Cakmak


Joseph M. Newcomer

unread,
Dec 5, 2001, 11:42:03 PM12/5/01
to
It would be a Bad Idea to catch this in the PreTranslateMessage. This is a misuse of
PreTranslateMessage. All you need to do is add to the message map the entry
ON_MESSAGE( (WM_APP+1), OnMyMessage)
(well, presumably you have done something like
#define UWM_MYMESSAGE (WM_APP+1)
but you get the idea).

Yes, SendMessage sends the message and calls the handler directly; PostMessage queues the
message for later processing. A thread should only use PostMessage.
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www3.pgh.net/~newcomer
MVP Tips: http://www3.pgh.net/~newcomer/mvp_tips.htm

Rufus V. Smith

unread,
Dec 10, 2001, 12:46:48 PM12/10/01
to

"Joseph M. Newcomer" <newc...@flounder.com> wrote in message
news:8ktt0ucklih5sddo7...@4ax.com...

>
> Yes, SendMessage sends the message and calls the handler directly; PostMessage queues the
> message for later processing. A thread should only use PostMessage.
> joe
>

On a different message thread in probably a different newsgroup
someone made a good additional observation about SendMessage
versus PostMessage.

SendMessage will be processed in the calling thread's context.

PostMessage will be processed in the receiving thread's context.

An important distinction.

Rufus

Jay Nabonne

unread,
Dec 10, 2001, 2:27:17 PM12/10/01
to

"Rufus V. Smith" <nospam@nospam> wrote in message
news:OAkVZmagBHA.2004@tkmsftngp07...

I don't think so. Message handlers *always* execute in the thread that's
running the message loop. From the SendMessage doc's Remarks section (*
emphasis mine):

<begin quote>
If the specified window was created by the calling thread, the window
procedure is called immediately as a subroutine. If the specified window was
created by a different thread, the system *switches to that thread* and
calls the appropriate window procedure. Messages sent between threads are
processed only when the receiving thread executes message retrieval code.
The sending thread is blocked until the receiving thread processes the
message.
<end quote>

That's the beauty of message loops. You're guaranteed that you're running in
the thread you're supposed to be.

--
Jay

Doug Harrison [MVP]

unread,
Dec 10, 2001, 2:42:30 PM12/10/01
to
Rufus V. Smith wrote:

>On a different message thread in probably a different newsgroup
>someone made a good additional observation about SendMessage
>versus PostMessage.
>
>SendMessage will be processed in the calling thread's context.
>
>PostMessage will be processed in the receiving thread's context.
>
>An important distinction.

It sounds like you're talking about inter-thread messages, and
inter-thread SendMessages are processed by the receiving thread.
Besides message queue usage, the important distinction between
SendMessage and PostMessage is that SendMessage is synchronous, even
inter-thread SendMessage. Thus, the initiator of an inter-thread
SendMessage blocks until the target thread enters a receptive state
and fully processes the message. This is explained further in MSDN.
(An inter-thread SendMessage often consists of sending a message to a
window created by another thread; the thread which created a window is
the only thread which can process that window's messages.)

--
Doug Harrison [VC++ MVP]
Eluent Software, LLC
http://www.eluent.com
Tools for Visual C++ and Windows

Rufus V. Smith

unread,
Dec 10, 2001, 2:44:16 PM12/10/01
to

"Jay Nabonne" <j...@rightagain.com> wrote in message news:u2fNDDbgBHA.2212@tkmsftngp03...

Well, that's what I thought in the first place!

It didn't make sense to me that your message handler would have to concern
itself as to whether the message had been Post'ed or Sent to it!

Now I have to go back to that font of misinformation!

(It would certainly be easy enough to test by doing a GetCurrentThreadID
within the message handler...)

Rufus

Rufus V. Smith

unread,
Dec 10, 2001, 2:51:56 PM12/10/01
to
"Doug Harrison [MVP]" <d...@mvps.org> wrote in message
news:sq3a1u0pthegetphm...@4ax.com...

> Rufus V. Smith wrote:
>
> >On a different message thread in probably a different newsgroup
> >someone made a good additional observation about SendMessage
> >versus PostMessage.
> >
> >SendMessage will be processed in the calling thread's context.
> >
> >PostMessage will be processed in the receiving thread's context.
> >
> >An important distinction.

if true, but it's not true!!

>
> It sounds like you're talking about inter-thread messages, and
> inter-thread SendMessages are processed by the receiving thread.

Thanks Doug,
Jay has also pointed this out and I gave a response then.

The discussion question was "VC++ has something like Timer control of VB??"

Rufus


Tron23

unread,
Dec 11, 2001, 11:51:14 PM12/11/01
to
    Does any one know how, from within your CDocument class, how you would get information (lets say a vector member variable) from the CView class.

Thank you

Eddy Pazz

unread,
Dec 12, 2001, 1:56:13 AM12/12/01
to
Somewhere in you CDocument class, do this:

POSITION pos = this->GetFirstViewPosition();
while(pos)
{
CView *pView = this->GetNextView( &pos );
if(pView->IsKindOf(RUNTIME_CLASS(CYourView)))
{
CYourView *pMyView = (CYourView *)pView;
// Now you get to your vector variable in the view
}
}

There's probably a better way to do this, so I welcome suggestions as well.

"Tron23" <tro...@qwest.net> wrote in message
news:3C16E24...@qwest.net...

0 new messages