type
TThread1 =
class(TThread)
public
MainWnd:hwnd;
otherThread:TThread;
procedure Terminate;
protected
procedure execute;
override;
end;
TThread2 =
class(TThread)
public
MainWnd:hwnd;
otherThread:TThread;
procedure Terminate;
protected
procedure execute;
override;
private
FTimer:TTimer;
procedure TimerTimer(Sender:TObject);
end;
TForm1 =
class(TForm)
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private-Deklarationen }
Thread1:TThread1;
Thread2:TThread2;
procedure ThreadMessage(
var msg:TMessage);
message WM_User;
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TThread1 }
procedure TThread1.execute;
var msg:TMsg;
begin
while getmessage(msg,0,0,0)
do
begin
postmessage(MainWnd,WM_User,0,1);
postThreadmessage(otherThread.ThreadID,WM_user,0,0);
Dispatchmessage(msg);
end;
end;
procedure TThread1.Terminate;
begin
inherited;
PostThreadMessage(ThreadID,WM_Quit,0,0);
end;
{ TThread2 }
procedure TThread2.execute;
var msg:TMsg;
begin
FTimer:=TTimer.Create(
nil);
FTimer.Interval:=1000;
FTimer.OnTimer:=TimerTimer;
FTimer.Enabled:=true;
while getmessage(msg,0,0,0)
do
begin
postmessage(MainWnd,WM_User,0,2);
Dispatchmessage(msg);
end;
end;
procedure TThread2.Terminate;
begin
inherited;
PostThreadMessage(ThreadID,WM_Quit,0,0);
end;
procedure TThread2.TimerTimer(Sender: TObject);
begin
postmessage(MainWnd,WM_user,0,0);
PostThreadMessage(otherThread.ThreadID,WM_User,0,0);
end;
{ TForm1 }
procedure TForm1.ThreadMessage(
var msg: TMessage);
begin
memo1.lines.add(inttostr(msg.lparam));
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Thread1:=TThread1.Create(true);
Thread2:=TThread2.Create(true);
Thread1.otherThread:=Thread2;
Thread2.otherThread:=Thread1;
Thread1.MainWnd:=
handle;
Thread2.MainWnd:=
handle;
Thread1.Resume;
Thread2.Resume;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
Thread1.Terminate;
Thread2.Terminate;
Thread1.WaitFor;
Thread2.WaitFor;
Thread1.Free;
Thread2.Free;
end;