H
hegyi
Unregistered / Unconfirmed
GUEST, unregistred user!
我在程序中定义了以下类
TSimpleClient = class(TObject)
UserID:Integer;
timerRemain:integer;
Threadointer;
end;
有一个ListView,以下是增加项的过程,目的是将Treeview的Item与线程互相关联
procedure TServerMgmt.AddAUser(ComBlock:TComBlock;AThread: TIdPeerThread);
var
l:TlistItem;
Client:TsimpleClient;
begin
……
l:=lvUsers.Items.Add;
Client:=TSimpleClient.Create;
client.timerRemain:=20;
Client.Thread:=Athread;
l.Caption:=inttostr(comBlock.SendID)
//列出在线人员的ID号
……
l.Data:=pointer(Client); //挂到Item上,便于通过Item直接找到连接线程(Athread)
Athread.Data:=l
//以便根据Athread可以直接找到Item
end;
在Listview的Item删除时,应当清理内存:
procedure TServerMgmt.lvusersDeletion(Sender: TObject
Item: TListItem);
var
Client:TSimpleClient;
aThread : TIdpeerThread ;
begin
if item.Data=nil then exit;
Client:=TSimpleClient(Item.data) ; //应当是Client:=Pointer(Item.data)?
aThread:=TIdpeerThread(Client.Thread);
aThread.Connection.disConnect;
aThread.Data:=nil;//aThread.data实际上应当等于Item,去掉关联。也许没必要。
Client.Thread:=nil;
Client.Free; //这里报错!
item.Data:=nil;
end;
TSimpleClient = class(TObject)
UserID:Integer;
timerRemain:integer;
Threadointer;
end;
有一个ListView,以下是增加项的过程,目的是将Treeview的Item与线程互相关联
procedure TServerMgmt.AddAUser(ComBlock:TComBlock;AThread: TIdPeerThread);
var
l:TlistItem;
Client:TsimpleClient;
begin
……
l:=lvUsers.Items.Add;
Client:=TSimpleClient.Create;
client.timerRemain:=20;
Client.Thread:=Athread;
l.Caption:=inttostr(comBlock.SendID)
//列出在线人员的ID号
……
l.Data:=pointer(Client); //挂到Item上,便于通过Item直接找到连接线程(Athread)
Athread.Data:=l
//以便根据Athread可以直接找到Item
end;
在Listview的Item删除时,应当清理内存:
procedure TServerMgmt.lvusersDeletion(Sender: TObject
Item: TListItem);
var
Client:TSimpleClient;
aThread : TIdpeerThread ;
begin
if item.Data=nil then exit;
Client:=TSimpleClient(Item.data) ; //应当是Client:=Pointer(Item.data)?
aThread:=TIdpeerThread(Client.Thread);
aThread.Connection.disConnect;
aThread.Data:=nil;//aThread.data实际上应当等于Item,去掉关联。也许没必要。
Client.Thread:=nil;
Client.Free; //这里报错!
item.Data:=nil;
end;