title: Delphi tIdTCPClient with timer events and other multi-threaded client side events
body: <p>We have a Delphi client server application using INDY.  The client has a single tIdTCPClient connection to the server which is multi threaded.  The client is "theoretically" a single thread.  But in practice there are multiple threads on the client and this is where my problem is.  For example think of a timer that fires every minute to get data from the server.  And consider what happens when a user runs a command at the same time as this timer event.  In truth, my problem is caused by our "Report Builder" reporting tool that (annoyingly) insists on loading every page of a report, which takes a while.  The report runs off our "special" dataset that has a caching mechanism to transmit batches of records at a time (so multiple calls to the server to get all data).  Meanwhile if a user does something else at the same time we seem to be getting crossed data.  It seems the user getting back data that was meant for the report.</p>
  
  <p>By the way, this bug is extremely rare, but a lot less rare for one particular customer who has the worlds slowest internet (luck me - I now have a test environment).</p>
  
  <p>So on the client I have code a bit like this... </p>
  
  <pre><code>procedure DoCommand(MyIdTCPClient:tIdTCPClient; var DATA:tMemoryStream);
  var
    Buffer: TBytes;
    DataSize: Integer;
    CommsVerTest: String;
  begin
    //Write Data
    MyIdTCPClient.IOHandler.Write(DATA.Size);
    MyIdTCPClient.IOHandler.Write(RawToBytes(Data.Memory^,DataSize));
  
    //Read back 6 bytes CommsVerTest should always be the same (ie ABC123)
    SetLength(Buffer,0); //Clear out buffer
    MyIdTCPClient.IOHandler.ReadBytes(Buffer,6); 
    CommsVerTest:=BytesToString(Buffer);
    if CommsVerTest&lt;&gt;'ABC123' then
      raise exception.create('Invalid Comms');      //It bugs out here in rare cases
  
    //Get Result Data Back from Server
    DataSize:=MyIdTCPClient.IOHandler.ReadLongInt;   
    Data.SetSize(DataSize);                         //Report thread is stuck here
    MyIdTCPClient.IOHandler.ReadBytes(Buffer,DataSize);
  end; 
  </code></pre>
  
  <p>Now when I debug it, I can confirm it bugs out when there are two threads in the middle of this procedure.  The main thread stops at the exception.  And the report thread is stuck somewhere else in the same procedure.</p>
  
  <p>So, it looks to me like I need to make the procedure above thread safe.
  I mean so if the user wants to do something they just have to wait until the report thread finishes.</p>
  
  <p>Arrrgh, I thought my client application was single threaded for sending data to the server!</p>
  
  <p>I think that using TThread would not work  - because I don't have access to the thread inside Report Builder.  I think I need a tCriticalSection.  </p>
  
  <p>I think I need to make the application so that the above procedure can only be run by one thread at a time.  Other threads have to wait.</p>
  
  <p>Someone please help with the syntax.</p>