PostMessage(HWND_BROADCAST, ...) gets handled 4 time in my ComPost OnMessage han

Giganews Newsgroups
Subject: PostMessage(HWND_BROADCAST, ...) gets handled 4 time in my ComPost OnMessage han
Posted by:  Warrick Wilson (warric…@mercuryonline.com)
Date: Fri, 1 Sep 2006

I've got 2 applications and I'm trying to get behavior working between them
using messages. I have one application monitoring the com port (using the
TComPort component), and I have a second application that just has a button
on it for sending a reply message.

When my ComPort app sees a particular incoming character, it does a
PostMessage(HWND_BROADCAST, MYMSG__SETSTATUS, 0, 0) using a message value
created by a RegisterWindowsMessage() call. The second app uses the same
message value, sees it, and activates a button. When I click that button in
the second app, it does a PostMessage(HWND_BROADCAST, MYMSG_REPLYSTATUS, 0,
0) that the ComPort app sees. When that message is received, the ComPort app
is supposed to send a particular character out of the com port.

What's happening is that my reply triggers 4 outputs from the com port app.
It's always 4, and I can trace with the debugger that it's always 4 times.
I'm just not sure why.

I've created a new OnMessage handler for each of my test apps that looks for
the message in question, and if found, take the appropriate action and sets
Handled := true;

procedure TForm1.AppMessage(var Msg: TMsg; var Handled: Boolean);
begin
  if Msg.message = MYMSG_REPLYSTATUS then
    begin
      try
        SendComPortSignal;
      except
        //TODO - handle any exception
      end;
      Handled := true;
    end;
end;

I had tried this originally with two basic apps, one with a button to post
the first message, one with a button to post the second message. It worked
fine, and when running in the debugger, it shows only one execution of the
reply handling code. But when the ComPort is involved, this AppMessage()
gets hit 4 times.

Can anyone shed any light on why this is happening? It's my first time using
TComPort, so maybe I've got something messed up with that.

Thanks.

Replies