Newsgroups : Borland : borland.public.delphi.nativeapi.win32 : 2006 Oct : How to: Block a thread until message arrives in message queue?
| Subject: | How to: Block a thread until message arrives in message queue? |
| Posted by: | "Arthur Hoornweg" (antispam.hoornweg@casema.nl.net) |
| Date: | Wed, 18 Oct 2006 15:23:05 |
Hello everybody,
I have the problem that one of my threads must wait until a
message for one of its Windows handles is in the message
queue. It is a blocking thread that uses a non-blocking component
that needs a message queue to work properly.
I want the thread to consume as little time as possible, but I also
want it to respond as quickly as possible to a new message.
My current message handler looks like the one below.
I've inserted the line "if not havemsg then sleep (20)"
to reduce CPU time; CPU time is as high as 100%
without it when I have a handful of these threads waiting.
My problem is now that the thread responds rather sluggishly
to messages.
I'd rather use one of the Windows "event" functions, such as
WaitForSingleObject (MessageArrived, 20) but I don't know
if that can be applied to windows messages and how.
Can anyone enlighten me?
Procedure tmyThread.Messagepump(sender: tobject);
Var
Msg: TMsg;
HaveMsg:Boolean;
Begin
Havemsg:=false;
While PeekMessage(Msg, 0, 0, 0, PM_REMOVE) Do
Begin
TranslateMessage(Msg);
DispatchMessage(Msg);
Havemsg:=true;
End;
if not havemsg then sleep(20);
End;
--
Arthur Hoornweg
(In order to reply per e-mail, please just remove the ".net"
from my e-mail address. Leave the rest of the address intact
including the "antispam" part. I had to take this measure to
counteract unsollicited mail.)