我想根添加到VirtualTreeView http://www.delphi-gems.com/index.php/controls/virtual-treeview像这样一个主题:
function AddRoot ( p : TForm1 ) : Integer; stdcall;
begin
p.VirtualStringTree1.AddChild(NIL);
end;
var
Dummy : DWORD;
i : Integer;
begin
for i := 0 to 2000 do begin
CloseHandle(CreateThread(NIL,0, @ADDROOT, Self,0, Dummy));
end;
end;
这样做的原因是,我想从我的INDY服务器添加到TreeView的所有连接。 Indy的onexecute /的onConnect得到的称为一个线程。 所以,如果3+连接在同一时间的应用程序崩溃进来,由于到TreeView。 同样的是,如果一个客户端断开连接,我想删除的节点。
我使用了Delphi7和Indy9
不知道如何解决这个问题?
编辑:
procedure TForm1.IdTCPServer1Disconnect(AThread: TIdPeerThread);
begin
VirtualStringTree1.DeleteNode(PVirtualNode(Athread.Data)); // For Disconnection(s)
end;
procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
Athread.Data := TObject(VirtualStringTree1.AddChild(NIL)); // For Connection(s);
end;
它工作正常的ListView(至少更好)。
编辑:这是我的全码:
服务器:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, IDSync, IdBaseComponent, IdComponent, IdTCPServer,
VirtualTrees;
type
TForm1 = class(TForm)
IdTCPServer1: TIdTCPServer;
VirtualStringTree1: TVirtualStringTree;
procedure FormShow(Sender: TObject);
procedure IdTCPServer1Connect(AThread: TIdPeerThread);
procedure IdTCPServer1Disconnect(AThread: TIdPeerThread);
private
{ Private declarations }
public
{ Public declarations }
end;
type
TAddRemoveNodeSync = class(TIdSync)
protected
procedure DoSynchronize; override;
public
Node : PVirtualNode;
Adding : Boolean;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TAddRemoveNodeSync.DoSynchronize;
begin
if Adding then
Node := Form1.VirtualStringTree1.AddChild(nil)
else
Form1.VirtualStringTree1.DeleteNode(Node);
end;
procedure TForm1.FormShow(Sender: TObject);
begin
IDTCPServer1.DefaultPort := 8080;
IDTCPServer1.Active := TRUE;
end;
procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
begin
with TAddRemoveNodeSync.Create do
try
Adding := True;
Synchronize;
AThread.Data := TObject(Node);
finally
Free;
end;
end;
procedure TForm1.IdTCPServer1Disconnect(AThread: TIdPeerThread);
begin
with TAddRemoveNodeSync.Create do
try
Adding := False;
Node := PVirtualNode(AThread.Data);
Synchronize;
finally
Free;
AThread.Data := nil;
end;
end;
end.
客户(Stresser)
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils,
Windows,
Winsock;
Const
// Connection Vars
Port = 8080;
Host = '127.0.0.1';
StressDelay = 1; // Miliseconds!
var
WSA : TWSADATA;
MainSocket : TSocket;
Addr : TSockAddrIn;
begin
if WSAStartup($0202, WSA) <> 0 then exit;
Addr.sin_family := AF_INET;
Addr.sin_port := htons(Port);
Addr.sin_addr.S_addr := INET_ADDR(Host);
while true do begin
MainSocket := Socket(AF_INET, SOCK_STREAM, 0);
Connect(MainSocket, Addr, SizeOf(Addr));
CloseSocket(MainSocket); // Disconnect!
sleep (StressDelay);
end;
end.