How do I PostMessage (PostThreadMessage?!?) from a worker thread to a TComponent descendent?

Hi!  I want to create a custom component (derived from TComponent) that spawns a worker thread which uses PostMessage or PostThreadMessage to asynchronously report progress in a non-blocking manner back to the custom component.  I can't use synchronization as I need the worker thread to run at speed.

I've read through dozens of "answers" on the subject on this board but all of the answers are incomplete or fragmentary.  I'd appreciate a COMPLETE source-code example since there's obviously something subtle I'm missing.  Thanks....
lsbermanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BlackTigerXCommented:
do you have some code and are getting stuck somewhere?

you will get better responses if you ask specific questions on something that you don't know how to implement or is not working as you would expect, rather than asking for someone to create the whole thing for you

if you have some code post it here and we can go from there...
0
Wim ten BrinkSelf-employed developerCommented:
Are you sending the messages to the application handle, the form handle or have you written your own, personal message handler?

Advise... Create a small application that does something similar to what you want to do: a separate thread sending messages to some component. If you don't get it to work, post the code here... The code below is tested on my system and works just fine. (But it could be that your component isn't receiving any messages!)

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

const
  WM_ThreadMessage = WM_USER + 100;

type
  TCustomComponent = class( TWinControl )
  private
    FCount: Integer;
  protected
    procedure WMThreadMessage( var Message: TMessage ); message WM_ThreadMessage;
  public
    constructor Create( AOwner: TComponent ); override;
  end;
  TForm1 = class( TForm )
    procedure FormCreate( Sender: TObject );
  private
    CustomComponent: TCustomComponent;
  public
  end;
  TMessageThread = class( TThread )
  private
    FFormHandle: THandle;
  protected
    procedure Execute; override;
  public
    constructor Create( Handle: THandle );
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

constructor TCustomComponent.Create( AOwner: TComponent );
begin
  inherited;
  if ( AOwner is TWinControl ) then Parent := ( AOwner as TWinControl );
  TMessageThread.Create( Handle );
end;

procedure TCustomComponent.WMThreadMessage( var Message: TMessage );
begin
  Inc( FCount );
  Form1.Caption := Format( 'Time: %s, Count: %d.', [ FormatDateTime( 'HH:NN:SS.ZZZ', Now ), FCount ] );
end;

procedure TForm1.FormCreate( Sender: TObject );
begin
  CustomComponent := TCustomComponent.Create( Self );
  CustomComponent.FCount := 0;
end;

constructor TMessageThread.Create( Handle: THandle );
begin
  inherited Create( True );
  FFormHandle := Handle;
  FreeOnTerminate := True;
  Resume;
end;

procedure TMessageThread.Execute;
var
  I: Integer;
begin
  for I := 1 to 20 do begin
    Sleep( 1000 );
    PostMessage( FFormHandle, WM_ThreadMessage, 0, 0 );
  end;
end;

end.

Oh, you will need an empty new form for this... :-)
The problem you're having is that your component doesn't have any handle for it to receive messages from the system. Thus, TComponent isn't even able to handle incoming messages. To add support for messages you will have to add some message window to your component so it will receive messages.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.