R
ranyang
Unregistered / Unconfirmed
GUEST, unregistred user!
我利用线程做了个接受数据的程序源码如下:
program Project1;
uses
Forms,
Umain in 'Umain.pas' {Form1},
u_clientsocketthread in 'u_clientsocketthread.pas';
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
~~~~~~~~~~~~~
unit Umain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp, u_clientsocketthread;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
test: TclientsocketThread;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
try
//Test.Terminate;
Test.Destory;
//test:= TclientsocketThread.create('127.0.0.1',9110);
except
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
test:= TclientsocketThread.create('127.0.0.1',9110);
end;
procedure TForm1.FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
begin
Test.Destory;
//Test:=nil;
end;
end.
~~~~~~~~~~~~~~~~~~~~~~~
unit u_clientsocketthread;
interface
uses
Classes,Forms ,ScktComp,SysUtils;
type
TclientsocketThread = class(TThread)
private
str: string;
ClientSocket: TClientSocket;
{ Private declarations }
protected
procedure Execute;
override;
public
constructor Create(HostIP: string;HostPort: integer);//
destructor Destory;
procedure ClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocketConnecting(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocketError(Sender: TObject;Socket:
TCustomWinSocket;ErrorEvent: TerrorEvent;
var ErrorCode: Integer);
procedure ClientSocketConnect(Sender: TObject;Socket:
TCustomWinSocket);
end;
implementation
uses umain;
var
IsTrue: Boolean=False;
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure Tclientsocket.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ Tclientsocket }
constructor TclientsocketThread.Create(HostIP: string;HostPort: integer);
begin
//
inherited create(False);
ClientSocket := TClientSocket.Create(nil);
IsTrue:=True;
with ClientSocketdo
begin
Address := HostIP;
Port := HostPort;
ClientType := ctBlocking;
OnError := ClientSocketError;
OnConnect := ClientSocketConnect;
OnConnecting :=ClientSocketConnecting;
OnDisconnect :=ClientSocketDisconnect;
end;
ClientSocket.Active:=True;
end;
destructor TclientsocketThread.Destory;
begin
Clientsocket.Close;
Clientsocket.Free;
Clientsocket:=nil;
IsTrue:=False;
inherited;
end;
procedure TclientsocketThread.ClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
//
end;
procedure TclientsocketThread.ClientSocketConnecting(Sender: TObject;
Socket: TCustomWinSocket);
begin
end;
procedure TclientsocketThread.ClientSocketError(Sender: TObject;Socket:
TCustomWinSocket;ErrorEvent: TerrorEvent;
var ErrorCode: Integer);
begin
ClientSocket.Close;
ErrorCode := 0;
end;
procedure TclientsocketThread.ClientSocketConnect(Sender: TObject;Socket:TCustomWinSocket);
begin
try
ClientSocket.Socket.SendText('<USER_NAME>z1</USER_NAME>');
except
end;
end;
procedure Tclientsocketthread.Execute;
begin
while (not Terminated) and (IsTrue=True)do
begin
str:= Clientsocket.socket.ReceiveText;
Form1.ListBox1.Items.Add(str);
end;
end;
end.
~~~~~~~~~~~~~~
现在的问题是当结束线程时会出现问题!!!大家帮帮忙
program Project1;
uses
Forms,
Umain in 'Umain.pas' {Form1},
u_clientsocketthread in 'u_clientsocketthread.pas';
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
~~~~~~~~~~~~~
unit Umain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ScktComp, u_clientsocketthread;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
test: TclientsocketThread;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
try
//Test.Terminate;
Test.Destory;
//test:= TclientsocketThread.create('127.0.0.1',9110);
except
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
test:= TclientsocketThread.create('127.0.0.1',9110);
end;
procedure TForm1.FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
begin
Test.Destory;
//Test:=nil;
end;
end.
~~~~~~~~~~~~~~~~~~~~~~~
unit u_clientsocketthread;
interface
uses
Classes,Forms ,ScktComp,SysUtils;
type
TclientsocketThread = class(TThread)
private
str: string;
ClientSocket: TClientSocket;
{ Private declarations }
protected
procedure Execute;
override;
public
constructor Create(HostIP: string;HostPort: integer);//
destructor Destory;
procedure ClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocketConnecting(Sender: TObject;
Socket: TCustomWinSocket);
procedure ClientSocketError(Sender: TObject;Socket:
TCustomWinSocket;ErrorEvent: TerrorEvent;
var ErrorCode: Integer);
procedure ClientSocketConnect(Sender: TObject;Socket:
TCustomWinSocket);
end;
implementation
uses umain;
var
IsTrue: Boolean=False;
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure Tclientsocket.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ Tclientsocket }
constructor TclientsocketThread.Create(HostIP: string;HostPort: integer);
begin
//
inherited create(False);
ClientSocket := TClientSocket.Create(nil);
IsTrue:=True;
with ClientSocketdo
begin
Address := HostIP;
Port := HostPort;
ClientType := ctBlocking;
OnError := ClientSocketError;
OnConnect := ClientSocketConnect;
OnConnecting :=ClientSocketConnecting;
OnDisconnect :=ClientSocketDisconnect;
end;
ClientSocket.Active:=True;
end;
destructor TclientsocketThread.Destory;
begin
Clientsocket.Close;
Clientsocket.Free;
Clientsocket:=nil;
IsTrue:=False;
inherited;
end;
procedure TclientsocketThread.ClientSocketDisconnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
//
end;
procedure TclientsocketThread.ClientSocketConnecting(Sender: TObject;
Socket: TCustomWinSocket);
begin
end;
procedure TclientsocketThread.ClientSocketError(Sender: TObject;Socket:
TCustomWinSocket;ErrorEvent: TerrorEvent;
var ErrorCode: Integer);
begin
ClientSocket.Close;
ErrorCode := 0;
end;
procedure TclientsocketThread.ClientSocketConnect(Sender: TObject;Socket:TCustomWinSocket);
begin
try
ClientSocket.Socket.SendText('<USER_NAME>z1</USER_NAME>');
except
end;
end;
procedure Tclientsocketthread.Execute;
begin
while (not Terminated) and (IsTrue=True)do
begin
str:= Clientsocket.socket.ReceiveText;
Form1.ListBox1.Items.Add(str);
end;
end;
end.
~~~~~~~~~~~~~~
现在的问题是当结束线程时会出现问题!!!大家帮帮忙