[DUG] Windows Messages in a Thread

Corey Murtagh emonk at xtra.co.nz
Mon Jul 18 16:35:53 NZST 2005


Ross Levis wrote:

> Similar to what I needed help with a few days ago.  I would like to set 
> up a thread to handle messages posted from a DLL using PostMessage.  
> I've created a thread object with an extra variable to store the handle 
> from AllocateHWnd, and assigned this to the DLL's parent handle.
>  
> I'm not sure whether I need to provide an Thread.Execute procedure since 
> I'm not executing anything.  I currently don't have one and I've set 
> FreeOnTerminate to False.
>  
> Messages are being received by the WndProc procedure defined in the 
> thread object but I'm not so sure that messages are being processed as 
> they arrive.  I may be wrong but I'm wondering if the WndProc 
> procedure is executing in the main thread.
>  
> Is what I have done sound right?

Sorry Ross, I haven't been paying much attention to the list recently. 
Surprised you didn't get more response to this one.

In case you haven't already solved this problem by yourself, here are 
some thoughts...

When Thread.Execute finishes, the thread terminates.  Not having a 
Thread.Execute means that your thread will be VERY short-lived.

Messages get processed in the context of the thread that created the 
handle.  If you create a handle in the main thread, then that's the 
context the messages get processed in.  So you need to call AllocateHWnd 
in the Thread.Execute - NOT in the Create or anything else that might 
get executed from the main thread.

You need a message loop.  This is nicely abstracted in the depths of the 
VCL - and I presume the CLX - but you need to do it manually in your own 
thread or you won't get anything.  In a thread the message loop needs to 
watch for quit messages and so on, check for thread termination 
requests, call Translate and Dispatch to get messages to their 
respective windows, etc.  You need to be aware of both Thread messages 
and Window messages.

Hope some of that helps.  I'd give you some code, but I'm an evil C++ 
programmer, so I probably wouldn't be of much help there :>

-- 
Corey Murtagh
The Electric Monk
"Quidquid latine dictum sit, altum viditur!"


More information about the Delphi mailing list