SendMessage only calls the target window's procedure directly if you
call it from the UI thread.  The Win32 documentation says that if you
send a message from some other thread, the message will only be
processed when the UI thread executes message retrieval code (i.e. its
message pump runs).  It works this way to ensure that any messages sent
to a window are always handled on the UI thread regardless of where they
were sent from.

This means that the code cannot run during other event handlers unless
the UI thread explicitly allows it.  Of course, the UI thread could do
that by calling DoEvents, as you say, but unless I've misunderstood you,
you seem to be suggesting that because PostMessage is being used for
[Begin]Invoke, these methods won't be run during DoEvents handling.  But
doesn't DoEvents process all messages, regardless of whether they were
sent with PostMessage or SendMessage?  I thought DoEvents simply
serviced the message queue, which means it'll handle messages sent with
SendMessage AND messages posted with PostMessage.  (So if you call
DoEvents, you would expect calls made either by Invoke and BeginInvoke
to be handled in any case.  The fact that Invoke uses PostMessage rather
than using SendMessage doesn't change that does it?  I just wrote an
experiment that seems to confirm that - outstanding BeginInvoke calls do
seem to be handled during DoEvents.)

But I agree that it could change the order in which things are handled.
Messages sent via SendMessage aren't technically 'queued' messages, and,
as I understand it, they get dealt with when you call GetMessage,
whereas queued messages are handled when you call DispatchMessage.  So I
see what you're saying about the processing being done ahead of those
made by BeginInvoke.

I'm wondering if that's the main reason.  Or if it's just that using
PostMessage for both enables them to share the code.  (The internal
implementation of these methods appears to boil down to the same private
method, passing in either True or False for the 'synchronous'
parameter.)

-- 
Ian Griffiths - DevelopMentor
http://www.interact-sw.co.uk/iangblog/

> -----Original Message-----
> From: J. Merrill
> 
> It's often important -- using SendMessage (which directly calls the
> target's window procedure, and blocks until it returns) would make
Invoke
> calls be processed ahead of other calls made by BeginInvoke, and
> potentially run _during_ other UI-code event handlers that happened to
> call DoEvents().
> 
> At 02:18 PM 3/1/2004, Dmitriy Zaslavskiy wrote
> >I think they both use PostMessage and Control.Invoke waits on event.
> >This difference is *almost* never makes a difference.

===================================
This list is hosted by DevelopMentorŪ  http://www.develop.com
Some .NET courses you may be interested in:

NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to