Link to home
Start Free TrialLog in
Avatar of TicketToTotalitarianism
TicketToTotalitarianism

asked on

thread messagepump

This should be easy... but I can't get it working :(

mainform and a thread named clientsocket.

This is the execute loop from the thread:

while (ClientSocket.Connected) and (not Terminated) do
  try
    while {something} do
      DispatchRecord;
    SendRecord;
    SleepEx(1,true);
 except
   on E: EIdReadTimeout do SendRecord;
 end;

SendRecord does not much more then this:
  While PeekMessage(MSg,0,0,0,PM_REMOVE) do
    {1: use message to send something)

also tried: PeekMessage(MSg,INVALID_HANDLE_VALUE,0,0,PM_REMOVE)

But I never get to point 1 !! The message is sent wrongly or the peekmessage loop is not OK.

Here is how I sent a message to the thread:
    PostThreadMessage(ClientSocket.Handle,WM_Client_Data,integer(DataOut),0);
also tried:
    PostMessage(ClientSocket.Handle,WM_Client_Data,integer(DataOut),0);


I think it's something easy I just forgot. what am I doing wrong?

SOLUTION
Avatar of jsmugen
jsmugen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TicketToTotalitarianism
TicketToTotalitarianism

ASKER

I tried
PeekMessage(msg, 0, WM_USER, WM_USER, PM_NOREMOVE);
as first thing in the thread.execute function.

and with PostThreadMessage..

but no succes :(

Can somebody give me a working small example? I can't find it on the net.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I've found the error..
PostThreadMessage needs a THREADID !.. not a ThreadHandle.
I knew it was something easy.. :D

Emadat, You're creating a window-handle with your thread, so that thread is the owner of it. When you postmessage to that handle, windows sends it to the wndproc of the owner of the window-handle, in this case your thread.

But I have a loop which doesn't only handles messageges.

This is a working example of a thread-messagepump and how to send messages to it.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,unit2, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    test : TTest;// the test-thread
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  test := TTest.Create(false);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  PostThreadMessage(test.ThreadID,WM_USER,0,0);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  test.Terminate;
end;

end.

// the thread-class
unit Unit2;

interface

uses
  Classes,windows,messages,Dialogs;

type
  TTest = class(TThread)
  private
    { Private declarations }
  protected
    procedure Execute; override;
  end;

implementation


procedure TTest.Execute;
var
  Msg : Tmsg;
begin
  FreeOnTerminate := true;
  while not terminated do
    While PeekMessage(MSg,0,0,0,PM_REMOVE) do
      messagebeep(1);
  messagebeep(1);
end;

end.


TicketToTotalitarianism:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
thanx for your time!