title: Delphi: How to wait all info to be read from socket using thread
body: <p>I have Delphi lib which must return information read via socket.</p>
  
  <pre><code>function GetBufferInfo(Address: PChar): PChar; export; stdcall;
  var
    BD: TBufferData;
  begin
    BD := TBufferData.Create;
    Result := PChar(TBufferData.GetData);
    BD.Free;
  end;
  </code></pre>
  
  <p>TBufferData class has a method ReadData which is being called when socket Read event fires. So it can be called several times until all info is read. The problem is how to wait while information is being read and don't go out of GetBufferInfo method. I thought about threads but don't know how exactly it can be done.</p>
  
  <p>I created a small example which demonstrates the issue:</p>
  
  <pre><code>program Project1;
  
  {$APPTYPE CONSOLE}
  
  uses
    SysUtils, Classes, Windows;
  
  type
    TBufferData = class
    private
      FResult: string;
    public
      constructor Create;
      procedure ReadData(Sender: TObject; Buf: string; var Size: Integer);
      function GetData: string;
    end;
  
  { TBufferData }
  
  var
    BD: TBufferData;
    s: string;
  { TBufferData }
  
  constructor TBufferData.Create;
  begin
    FResult := 'Some text received via socket';
  end;
  
  function TBufferData.GetData: string;
  begin
    Result := FResult;
  end;
  
  procedure TBufferData.ReadData(Sender: TObject; Buf: string; var Size: Integer);
  begin
    //info is being received from socket
    FResult := FResult + Buf;
  end;
  
  begin
    BD := TBufferData.Create;
    s := BD.GetData;
    Writeln(s);
    BD.Free;
    Readln;
  end.
  </code></pre>
  
  <p>Thanks in advance
  Yura</p>