0

i'm using AnonymousThreads and StringList to do http request behind a proxy server. i'm using the list to hold the proxies, i choose randomly a proxy from the list and use it. i keep adding strings to the list. after a while the threads stops, i'm creating the ssl and http components in for loop. i think the problem is caused by accessing the list from multiple threads. i tried using Pipleline threads from Omni Treads library. it greate. but its slow with proxies because it's creating task after another but not at the same time.. i need a simple alternative to StringList where i can access the list without causing the threads to stop.

Procedure GetData; 
....   
var
 list : TStrings;
....
for i := 0 to 10 do
 begin
  GetData;
 end;
    TForm1.GetData;
    var
      x1, x2 : Integer;
      G1 : String;
    begin

       try
           X1 := list.count
           X2 := RansomRange(0, X1);
           G1 := list.strings[X2];
           list.Delete(X2);


        lHTTP := TIdHTTP.Create(nil);
        IdSSL := TIdSSLIOHandlerSocketOpenSSL.Create(lHTTP);
        ....
       idssl.free;
       lhttpfree;

        except on E: EIdHTTPProtocolException do
          begin
            idssl.free;
              lhttpfree;
              list.add(X2);
           TThread.Synchronize (TThread.CurrentThread,
            procedure ()
              begin
               memo1.lines.add(E.ErrorMessage);
              end);
          end;

       end;
     end;
henry
  • 1
  • 1
  • Please be more specific and clear about your problem. Post some code, example etc. – mate00 Aug 15 '19 at 19:16
  • Updated @mate00 – henry Aug 15 '19 at 19:30
  • 1
    You need a lock around your list, such as a `TCriticalSection` or `TMutex`. Lock the list before reading/modifying its content, then unlock it. That will prevent concurrency issues using the list in multiple threads. Since you are using Indy for your HTTP work, you can use Indy's `TIdThreadSafeStringList` class in the `IdThreadSafe` unit for this purpose – Remy Lebeau Aug 16 '19 at 00:02
  • `TIdThreadSafeStringList ` is useful, but how i can choose randomly a string from it? there is no `count` or `Strings` properties in it @RemyLebeau – henry Aug 16 '19 at 07:59
  • @henry the `TIdThreadSafeStringList.Lock()` method returns a pointer to the underlying `TStringList` object after locking access to it. You can use that `TStringList` however you want, then `Unlock()` it when finished – Remy Lebeau Aug 16 '19 at 09:04
  • put your first comment as answer, `TIdThreadSafeStringList ` dose the required job @RemyLebeau – henry Aug 17 '19 at 14:16

0 Answers0