Board index » cppbuilder » Post Message Question....

Post Message Question....


2004-09-16 05:21:18 AM
cppbuilder14
I'm try to understand the use of PostMessage (with TForm and TThread).
In an application I have developed, I have a thread that puts data into a
queue (serial comms data), and then calls a PostMessage to the window to let
it know data is available.
The PostMessage is mapped to a method in the TMainForm class, and the method
takes data out of the queue, on receiving the PostMessage, and displays the
data etc.....
All seems to work okay, I don't have problems when its running, but
occassionally I get strange/bad behaviour on closing down.
I put some simple tracing in for the interfaces/event handlers, and what I
discovered was that when the TForm Close() event is being processed (waiting
for threads to terminate etc), the post messages were still being processed
in the appropriate method.
I wasn't expecting this. Is this correct? I know the event handlers are
effectively call backs, is this why?
If this is true, then I need to close the form down more cleanly.
 
 

Re:Post Message Question....

"PLM" < XXXX@XXXXX.COM >wrote:
Quote
I put some simple tracing in for the interfaces/event
handlers, and what I discovered was that when the
TForm Close() event is being processed (waiting for
threads to terminate etc), the post messages were
still being processed in the appropriate method.

I wasn't expecting this. Is this correct?
If you are using TThread::WaitFor() to wait for each thread to terminate, then yes. WaitFor() runs its own message pumping loop internally, so that SendMessage() (not PostMessage()) requests can still be processed. Otherwise, the code would deadlock. A thread could call SendMessage(), which would wait for the window to process the message, but the window would be blocked waiting for the thread to terminate and thus not be processing and new messages. They would wait on each other indefinately.
Gambit
 

Re:Post Message Question....

Thanks, that makes sense now. Back to the drawing board to do a bit of
rework.......
PS. Are there any other situations like this I need to be aware of where
there is internal message pumping for TThread and TForm event handling?
"Remy Lebeau [TeamB]" < XXXX@XXXXX.COM >wrote in message
Quote

"PLM" < XXXX@XXXXX.COM >wrote:

>I put some simple tracing in for the interfaces/event
>handlers, and what I discovered was that when the
>TForm Close() event is being processed (waiting for
>threads to terminate etc), the post messages were
>still being processed in the appropriate method.
>
>I wasn't expecting this. Is this correct?

If you are using TThread::WaitFor() to wait for each thread to terminate,
then yes. WaitFor() runs its own message pumping loop internally, so that
SendMessage() (not PostMessage()) requests can still be processed.
Otherwise, the code would deadlock. A thread could call SendMessage(),
which would wait for the window to process the message, but the window would
be blocked waiting for the thread to terminate and thus not be processing
and new messages. They would wait on each other indefinately.
Quote


Gambit
 

{smallsort}