Article View: borland.public.cppbuilder.nativeapi
Article #13781Re: PeekMessage/SendMessage and threads
From: nicolasr
Date: Fri, 12 Sep 2003 18:34
Date: Fri, 12 Sep 2003 18:34
63 lines
3432 bytes
3432 bytes
Hi,
I finally found another post in a Delphi group by Rob Kennedy (thanks!) that
seems to answer most of my questions. I will quote it here for others with
similar problems:
<quote>
valentin tihomirov wrote:
> function TThread.WaitFor: LongWord;
> var
> Msg: TMsg;
> H: THandle;
> begin
> H := FHandle;
> if GetCurrentThreadID = MainThreadID then
> while MsgWaitForMultipleObjects(1, H, False, INFINITE,
> QS_SENDMESSAGE) = WAIT_OBJECT_0 + 1 do PeekMessage(Msg, 0, 0, 0,
> PM_NOREMOVE)
> else WaitForSingleObject(H, INFINITE);
> GetExitCodeThread(H, Result);
> end;
>
> I suppose that main thread somehow prosesses messages. It should be done
to
> remain responsive to user and for the terminating thread that is more
> important to prevent deadlocks. I do not see code like this:
>
> while GetMessage {
> TranslateMessage();
> DispatchMessage()
> }
>
> What does effectively unlocks a thread sending a message to another?
When you call SendMessage, the message does not really get processed
immediately. Instead, the system will sometimes wait until you call
GetMessage or PeekMessage. When you do that, the system will deliver all
those sent messages before finally returning a new message from the
message queue. (Queued messages are posted, not sent.)
When TThread.WaitFor uses PeekMessage, it's relying on that secondary
message-delivery behavior, not the primary message-retreival, which is
evident since it uses the pm_NoRemove flag.
So the main thread calls MsgWaitForMultipleObjects and sets it to return
if there are any messages sent to a window in the main thread
(qs_SendMessage), and when that happens, PeekMessage delivers them.
It works this way to help prevent deadlock when WaitFor and Synchronize
are used in the same program. The Synchronize method is implemented by
having the TThread use SendMessage on the main thread. Without the
MsgWaitFormUltipleObject/PeekMessage combination, the main thread would
get stuck waiting for the TThread with WaitForSingleObject, and the
TThread would get stuck waiting for the main threa with SendMessage.
If TThread.WaitFor used the more conventional
Get/Translate/DispatchMessage loop, then it wouldn't really be doing
much "waiting" for the thread anymore.
--
Rob
</quote>
Technical Details
Message-ID:
<3f621290$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-06!sn-xit-08!supernews.com!priapus.visi.com!news-out.visi.com!hermes.visi.com!nycmny1-snh1.gtei.net!nycmny1-snf1.gtei.net!news.gtei.net!colt.net!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<3f5bb022@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10cc67,d49ccccc71a00b3e X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2003-09-12 11:40:09 PST From: "nicolasr" <nicolasrNOSPAMATSIGNgmx.net> Newsgroups: borland.public.cppbuilder.nativeapi References: <3f5bb022@newsgroups.borland.com> Subject: Re: PeekMessage/SendMessage and threads Date: Fri, 12 Sep 2003 20:34:29 +0200 Lines: 65 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 NNTP-Posting-Host: 80.133.172.10 Message-ID: <3f621290$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1063391888 80.133.172.10 (12 Sep 2003 11:38:08 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-06!sn-xit-08!supernews.com!priapus.visi.com!news-out.visi.com!hermes.visi.com!nycmny1-snh1.gtei.net!nycmny1-snf1.gtei.net!news.gtei.net!colt.net!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail