Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8180231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:10:15+00:00 2026-06-07T00:10:15+00:00

I would like to add roots to a VirtualTreeView http://www.delphi-gems.com/index.php/controls/virtual-treeview with a thread like

  • 0

I would like to add roots to a VirtualTreeView http://www.delphi-gems.com/index.php/controls/virtual-treeview with a thread like this:

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;

The reason for this is that I want to add all connections from my INDY Server to the TreeView. Indy’s onexecute/onconnect get’s called as a thread. So if 3+ connections come in at the same time the app crashes due to the TreeView. Same is if a client gets disconnected and I want to delete the Node.

I am using Delphi7 and Indy9

Any Idea how to fix that?

EDIT:

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;

It works fine with ListView (at least better).

EDIT: Here is my full code:

Server:

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.

Client (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.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T00:10:15+00:00Added an answer on June 7, 2026 at 12:10 am

    As you commented, TIdTCPServer is a multithreaded component. You must synchronize with the main thread in order to access the UI safely from the TIdTCPServer events. You can use Indy’s own TIdSync (synchronous) or TIdNotify (asynchronous) class for that purpose, eg:

    type
      TAddRemoveNodeSync = class(TIdSync)
      protected
        procedure DoSynchronize; override;
      public
        Node: PVirtualNode; 
        Adding: Boolean;
      end;
    
    procedure TAddRemoveNodeSync.DoSynchronize;
    begin
      if Adding then
        Node := Form1.VirtualStringTree1.AddChild(nil)
      else
        Form1.VirtualStringTree1.DeleteNode(Node);
    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; 
    

    Update: Based on new info, I would do something more like this instead:

    type
      TAddRemoveClientNotify = class(TIdNotify)
      protected
        fAdding: Boolean;
        fIP, fPeerIP: string;
        fPort, fPeerPort: Integer;
        ...
      public
        constructor Create(AThread: TIdPeerThread; AAdding: Boolean); reintroduce;
        procedure DoNotify; override;
      end;
    
    constructor TAddRemoveClientNotify.Create(AThread: TIdPeerThread; AAdding: Boolean);
    begin
      inherited Create;
      fAdding := AAdding;
      with AThread.Connection.Socket.Binding do
      begin
        Self.fIP := IP;
        Self.fPeerIP := PeerIP;
        Self.fPort := Port;
        Self.fPeerPort := PeerPort;
      end;
    end;
    
    procedure TAddRemoveClientNotify.DoNotify;
    var
      Node: PVirtualNode;
    begin
      if fAdding then
      begin
        Node := Form1.VirtualStringTree1.AddChild(nil);
        // associate fIP, fPeerIP, fPort, fPeerPort with Node as needed...
      end else
      begin
        // find the Node that is associated with fIP, fPeerIP, fPort, fPeerPort as needed...
        Node := ...;
        if Node <> nil then
          Form1.VirtualStringTree1.DeleteNode(Node);
      end;
    end;
    
    procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread); 
    begin 
      TAddRemoveClientNotify.Create(AThread, True).Notify;
    end; 
    
    procedure TForm1.IdTCPServer1Disconnect(AThread: TIdPeerThread); 
    begin 
      TAddRemoveClientNotify.Create(AThread, False).Notify;
    end; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to add an element node in my XML file using PHP.
I would like to add a memory appender to the root logger so that
I have own project and i would like add for this class same as
I have a ListView and each item have a TextView. I would like add
I would like to add a GestureDetector to all views (view groups) of an
I would like to add a custom calculation method to a managed object (which
I would like to add a hash into an array using Ruby version 1.8.7:
I would like to add up the number of line changes in an SVN
I would like to add a pop effect when I hover over a SVG
I would like to add a small dice-rolling effect to my Javascript code. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.