Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 Jan : TIdHTTP in multiple threads

www.cryer.info
Managed Newsgroup Archive

TIdHTTP in multiple threads

Subject:TIdHTTP in multiple threads
Posted by:"Ron" (nospam@please)
Date:Thu, 26 Jan 2006 14:15:46

I am starting a number of threads to retrieve HTML pages and then waiting
for threads to finish the work...Sometimes it works fine, sometimes the
while
loops infinitely...I tried to play with ConnectTimeout but it doesnt help...

How this should be done properly? D7, Indy 9.00.18:

        for vIdx := 0 to vLinks2.Count - 1 do
        begin
          vLink := vLinks2[vIdx];
          vThread := THTTPThread.Create(vLink, vDOB, vDOB2, frmMain);
          vThreadList.Add(vThread);
          vThread.FirstName := FirstName;
          vThread.LastName := LastName;
          vThread.Resume
        end;

        vReady := False;
        while not vReady do
        begin
          vReady := True;
          for vIdx := 0 to vThreadList.Count - 1 do
            vReady := vReady and THTTPThread(vThreadList[vIdx]).Suspended;

          Application.ProcessMessages; // I tried with and without
Application.ProcessMessages
    end;

Now the thread code:

constructor THTTPThread.Create(URL: String;  DOB: String; DOB2: String;
AForm: TForm);
begin
  inherited Create(True);
  FreeOnTerminate := True;
  FForm := AForm;
  FURL := URL;
  FDOB := DOB;
  FDOB2 := DOB2;
  FHTTP := TIdHTTP.Create(AForm);
  FHTTP.ConnectTimeout := 2000;
  FIOHandler := TIdSSLIOHandlerSocket.Create(AForm);
  FIncludeLink := True;
  FImageLink := ''
end;

procedure THTTPThread.Execute;
var
  vException: Boolean;
  vResult: String;
  vParser: TDIHtmlParser;
  vPlugin: TDIHtmlLinksPlugin;
  vTempFileName: String;
  vTemp: TStringList;
  vSL: TStringList;

  function ContainsDOB(const Page: String): Boolean;
  begin
    Result := False;
    Result := Result or (Pos('DATE OF BIRTH', UpperCase(Page)) > 0);
    Result := Result or (Pos('DOB', UpperCase(Page)) > 0);
    Result := Result or (Pos('BIRTHDATE', UpperCase(Page)) > 0);
    Result := Result or (Pos('BIRTHDAY', UpperCase(Page)) > 0);
  end;

begin

  vException := False;
  vSL := TStringList.Create;
  try
    try
      FHTTP.Post('XXXXX', vSL);

      FHTTP.Post('YYYYY', vSL);

      FHTTP.Get('ZZZZZZ');
    except
      on E: Exception do
        vException := True
    end
  finally
    vSL.Free
  end;

  if not vException then
  begin
    if Pos('https', FURL) > 0 then
      FHTTP.IOHandler := FIOHandler;

    vException := False;
    try
      vResult := FHTTP.Get(FURL);
    except
      on E: Exception do
      begin
        vException := True;
      end
    end;

    // ...some processing with vResult

  Suspend
end;

Replies:

www.cryer.info
Managed Newsgroup Archive