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_R EMOVE) do
{1: use message to send something)
also tried: PeekMessage(MSg,INVALID_HA NDLE_VALUE ,0,0,PM_RE MOVE)
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(ClientSo cket.Handl e,WM_Clien t_Data,int eger(DataO ut),0);
also tried:
PostMessage(ClientSocket.H andle,WM_C lient_Data ,integer(D ataOut),0) ;
I think it's something easy I just forgot. what am I doing wrong?
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_R
{1: use message to send something)
also tried: PeekMessage(MSg,INVALID_HA
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(ClientSo
also tried:
PostMessage(ClientSocket.H
I think it's something easy I just forgot. what am I doing wrong?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.Thr eadID,WM_U SER,0,0);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
test.Terminate;
end;
end.
// the thread-class
unit Unit2;
interface
uses
Classes,windows,messages,D ialogs;
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_R EMOVE) do
messagebeep(1);
messagebeep(1);
end;
end.
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
begin
PostThreadMessage(test.Thr
end;
procedure TForm1.FormDestroy(Sender:
begin
test.Terminate;
end;
end.
// the thread-class
unit Unit2;
interface
uses
Classes,windows,messages,D
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_R
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.
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.
ASKER
thanx for your time!
ASKER
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.