title: Indy10 File Transfer Causes 100% CPU Usage
body: <p>i managed to fix some errors with disconnecting, now whenever a file is transferring the CPU Usage becomes 100%, i dunno what im doing wrong :S.....</p>
  
  <pre><code>const
   MaxBufferSize = 1024;
  
  type
   TClient = class(TObject)
   public
    AContext: TIdContext;
    FileSize: Integer;
    Canceled: Boolean;
    Transfered: Integer;
    procedure ReceiveData;
    procedure Update;
   end;
  
  procedure TForm1.IdTCPServer1Execute(AContext: TIdContext);
  var
   Data: string;
   Client: TClient;
   Item: TListItem;
  begin
   Data := AContext.Connection.IOHandler.ReadLn;
  
   //Data := 'SEND|785548' = Command + | + FileSize
   if Copy(Data, 1, 4) = 'SEND' then
   begin
    Delete(Data, 1, 5);
    Client := TClient.Create;
    Client.FileSize := StrToInt(Data);
    Client.AContext := AContext;
    Item := ListView1.Items.Add;
    Item.Caption := AContext.Connection.Socket.Binding.PeerIP;
    Item.Data := Client;
    Client.ReceiveData;
   end;
  end;
  
  procedure TClient.ReceiveData;
  var
   currRead : Integer;
   FS: TFileStream;
  begin
   Canceled := False;
   FS := TFileStream.Create('C:\Test.dat', fmCreate or fmOpenReadWrite);
   FS.Size := 0;
   Transfered := 0;
   try
    while (FS.Position &lt; FileSize) and (Athread.Connection.Connected) and (not Canceled) do
    begin
     Application.ProcessMessages;
     if (FileSize - FS.Position) &gt;= MaxBufferSize then currRead := MaxBufferSize
     else currRead := (FileSize - FS.Position);
     AThread.Connection.IOHandler.ReadStream(FS, CurrRead);
     Transfered := FS.Position;
     Notify.NotifyMethod(Update);
     Application.ProcessMessages;
    end;
   finally
    FS.Free;
    AThread.Connection.IOHandler.InputBuffer.Clear;
    AThread.Connection.Disconnect;
    AThread.RemoveFromList;
    Notify.NotifyMethod(Update);
    Application.ProcessMessages;
   end;
  end;
  
  procedure TClient.Update;
  begin
   //Code to Display Progress bar and stuff (Simplified for now)
   Form1.Label1.Caption := 'Transfered Data : ' + IntToStr(Transfered);
  end;
  </code></pre>