0
votes

i need to process a received data from a tcp link.the data are frames of hex string at length of 203 bytes.

i save them at the end of tstringlist

MyList.Add( input );

and from a second thread read the first string and process it and remove firs from the list

procedure TMyThread.Execute;
 var str : string;
begin
    while not Terminated do
    begin
      FTermEvent.WaitFor(100);
      if not Terminated then
      begin
          str := MyList[0];     
          MyList.Delete(0);
          //some process
      end;
    end
end;

The question is , is this thread safe?!

1
Threading considerations aside, it would be more efficient to use a TQueue<string> instead of a TStringList. - Andreas Rejbrand
small nitpick, don't use strings to store binary data... - whosrdaddy
Please read this excellent primer on threading. What you have is a typical producer/consumer case... - whosrdaddy
why oh why would you store binary data as hex text? - David Heffernan

1 Answers

0
votes

If you are afraid to loose input data using database, you can try to use TThreadStringList. I imagine that your software receives data from multiple devices simultaneously (and in this case you should create a multi-thread socked to make you sure to receive all the data) if you are receiving data from a single device instead , you should make sure that the tcp protocol supports a sort of aknowlage system to avoid losing data or at least to report in a log, the data that your application has not been able to receive completely.

Anyway, TThreadStringList is a simple wrapper for TStringList. It should makes possible to access a String List from different threads without any conflicts. I can't test it but for you should be easy and quick to try.

The link: TThreadStringList