想请教一个问题:
是这样的,
如何关闭所有的线程?
尽量给些代码。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls;
type
TForm1 = class(TForm)
ServerSocket1: TServerSocket;
ClientSocket1: TClientSocket;
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Edit1: TEdit;
Button5: TButton;
procedure ServerSocket1GetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure ServerSocket1ThreadEnd(Sender: TObject;
Thread: TServerClientThread);
procedure ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
private
{ Private declarations }
public
{ Public declarations }
end;
TFileServerThread=Class(TServerClientThread)
public
procedure ClientExecute;override;
end;
var
Form1: TForm1;
a:TFileServerThread;
implementation
{$R *.dfm}
procedure TForm1.ServerSocket1GetThread(Sender: TObject;
ClientSocket: TServerClientWinSocket;
var SocketThread: TServerClientThread);
begin
//
a:=TFileServerThread.Create(False,ClientSocket);
SocketThread:=a;
Memo1.Lines.Add(IntToStr(ClientSocket.SocketHandle));
//ClientSocket.SendText('Welcome to Server'+#13#10);
end;
{ TFileServerThread }
procedure TFileServerThread.ClientExecute;
var
Data:Array [0..1023] of char;
RecText:string;
SocketStream:TWinSocketStream;
begin
While Not Terminated and ClientSocket.Connected do
try
SocketStream:=TWinSocketStream.Create(Clientsocket,30000);
FreeOnTerminate:=True;
try
FillChar(Data,Sizeof(Data),0);
if SocketStream.Read(Data,SizeOf(Data))=0 then
begin
ClientSocket.SendText('gsw798'+#13#10);
Sleep(1);
ClientSocket.Close;
Terminate;
end;
RecText:=Data;
if Length(RecText)>2 then
Delete(RecText,Pos(#13#10,RecText),2);
if ClientSocket.Connected then
begin
ClientSocket.SendText(RecText);
end;
finally
SocketStream.Free;
end;
except
synchronize(HandleException);
end;
ClientSocket.Close;
Terminate;
end;
procedure TForm1.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add(Socket.ReceiveText);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ServerSocket1.Open;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
str:string;
i:integer;
begin
TServerClientThread(a).Terminate;
TServerClientThread(a).Free;
sleep(5000);
if ServerSocket1.Socket.ActiveThreads=0 then
begin
showmessage('yes');
ServerSocket1.Close;
end
else
showmessage('no');
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
ClientSocket1.open;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
ClientSocket1.Close;
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
ClientSocket1.Socket.SendText(Edit1.Text);
end;
procedure TForm1.ServerSocket1ThreadEnd(Sender: TObject;
Thread: TServerClientThread);
begin
end;
procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add(IntToStr(Socket.SocketHandle))
end;
procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
// Memo1.Lines.Add(IntToStr(Socket.SocketHandle));
end;
end.