unit MainForm;
interface
uses
Windows, Messages, Graphics, Controls, Forms, Dialogs, ToolWin, ComCtrls,
Menus, Buttons, Spin, SysUtils, Classes, IdBaseComponent,
IdComponent, IdTCPConnection, IdTCPClient, ExtCtrls, StdCtrls,
IdAntiFreezeBase, IdAntiFreeze, IdIntercept;
type
TDLoadDirThread = class(TThread)
private
msg:string;
TheTCPClient :TIdTCPClient;
protected
procedure Execute; override;
procedure addmemLines;
procedure addc;
public
constructor Create(IdTCPClient:TIdTCPClient);
destructor Destroy; override;
end;
TForm1 = class(TForm)
Label1: TLabel;
edUserName: TEdit;
Label2: TLabel;
edServer: TEdit;
Label3: TLabel;
lbClients: TListBox;
Label4: TLabel;
memLines: TMemo;
Label5: TLabel;
edMessage: TEdit;
SpeedButton1: TSpeedButton;
IdTCPClient1: TIdTCPClient;
Timer1: TTimer;
Label6: TLabel;
sePort: TSpinEdit;
Button1: TButton;
IdAntiFreeze1: TIdAntiFreeze;
IdConnectionIntercept1: TIdConnectionIntercept;
Button2: TButton;
procedure FormShow(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure IdTCPClient1Connected(Sender: TObject);
procedure edMessageKeyPress(Sender: TObject; var Key: Char);
procedure Button1Click(Sender: TObject);
procedure IdConnectionIntercept1Receive(
ASender: TIdConnectionIntercept; AStream: TStream);
procedure IdConnectionIntercept1Connect(
ASender: TIdConnectionIntercept);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
CT:TDLoadDirThread;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
constructor TDLoadDirThread.Create(IdTCPClient:TIdTCPClient);
begin
inherited Create(True);
TheTCPClient :=IdTCPClient;
Execute;
// FreeOnTerminate :=False;
end;
procedure TDLoadDirThread.addmemLines;
begin
form1.memLines.Lines.Add(Msg);
end;
procedure TDLoadDirThread.addc;
begin
form1.lbClients.Items.CommaText := Msg;
end;
procedure TDLoadDirThread.Execute;
var com,s:string;
buf:array[0..2047] of char ;
begin
// try
while not (Terminated ) and TheTCPClient.Connected do
begin
TheTCPClient.ReadBuffer(buf,sizeof(buf));
s:=buf;
if s<>'' then
begin
//处理消息
Msg:=s;
if Msg[1] <> '@' then
begin
{ Not a system command }
self.Synchronize(addmemLines);
end
else
begin
{ System command }
Com := UpperCase(Trim(Copy(Msg, 2, Pos(':', Msg) -2)));
Msg := UpperCase(Trim(Copy(Msg, Pos(':', Msg) +1, Length(Msg))));
if Com = 'CLIENTS' then
self.Synchronize(addc);
end;
end;
end;
end;
destructor TDLoadDirThread.Destroy;
begin
inherited destroy;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
width := width + 1;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
Com,
Msg : String;
begin
// exit;
if not IdTcpClient1.Connected then
exit;
// if not (IdTCPClient1.ReadFromStack(false, 10, false)>0) then exit;
Msg := IdTCPClient1.ReadLn('', 5);
if Msg <> '' then
if Msg[1] <> '@' then
begin
{ Not a system command }
memLines.Lines.Add(Msg);
end
else
begin
{ System command }
Com := UpperCase(Trim(Copy(Msg, 2, Pos(':', Msg) -2)));
Msg := UpperCase(Trim(Copy(Msg, Pos(':', Msg) +1, Length(Msg))));
if Com = 'CLIENTS' then
lbClients.Items.CommaText := Msg;
end;
end;
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
if (edUserName.Text <> '') and
(edServer.Text <> '') and
SpeedButton1.Down then
begin
IdTCPClient1.Host := edServer.Text;
IdTCPClient1.Port := sePort.Value;
if SpeedButton1.Down then
begin
IdTCPClient1.Connect;
end;
end
else
begin
if (edUserName.Text = '') or
(edServer.Text = '') then
ShowMessage('You must put in a User Name and server name to connect.');
SpeedButton1.Down := false;
end;
end;
procedure TForm1.IdTCPClient1Connected(Sender: TObject);
begin
IdTCPClient1.WriteLn(edUserName.Text);
end;
procedure TForm1.edMessageKeyPress(Sender: TObject; var Key: Char);
begin
if key = #13 then
begin
IdTCPClient1.WriteLn(edMessage.Text);
edMessage.Text := '';
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
IdTCPClient1.WriteLn('@' + 'CLIENTS:REQUEST');
end;
procedure TForm1.IdConnectionIntercept1Receive(
ASender: TIdConnectionIntercept; AStream: TStream);
begin
showmessage('aaa');
end;
procedure TForm1.IdConnectionIntercept1Connect(
ASender: TIdConnectionIntercept);
begin
//showmessage('aaaaaaaa');
CT:=TDLoadDirThread.create(IdTcpClient1);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
ct.Terminate;
IdTCPClient1.Disconnect;
end;
end.
现在把原来的代码修改了一下,但是退出的时候线程关闭出错,信息是收到了,但无法在memo 上显示,开避了一个线程以后,在edit中输入都感到有点吃力,请大家再帮我看看,谢谢!