Newsgroups : Borland : borland.public.delphi.internet.winsock : 2005 May : TIdHttp: Calling Post and Disconnect on separate threads...

www.cryer.info
Managed Newsgroup Archive

TIdHttp: Calling Post and Disconnect on separate threads...

Subject:TIdHttp: Calling Post and Disconnect on separate threads...
Posted by:"Andrew Fiddian-Green" (..@dd)
Date:Fri, 20 May 2005 17:06:30

Hi!

I am running a TidHTTP Post on a secondary thread, and I have a procedure to
manually close the socket from the primary thread (see the code below).

1) If TmyThread.Cancel is called on thread #1 whilst thread #2 is in the
code blocks AAA or BBB, then it is possible that thread #2 might hit
FreeAndNil(fHttp) whilst thread #1 is inside fHttp.Disconnect (thus causing
an exception). => Is this a real risk with Indy? And if so, do I need to use
a critical section or something?

2) The fHttp.Disconnect on thread #1 should presumably cause the fHttp.Post
on thread #2 to exit with an exception; I never programmed anything before
where a call on one thread might cause an exception on another thread: => Is
this practice Ok?

procedure TmyThread.Execute;
begin
  fHttp := TIdHttp.Create(...
  try
    .. do stuff
    try
      ..
      fHttp.Post(...
      .. AAA
    except
      .. BBB
    end;
  finally
    FreeAndNil(fHttp);
  end;
end;

procedure TmyThread.Cancel;
begin
  if Assigned(fHttp) and fHttp.Connected then
    fHttp.Disconnect;
end;

Regards,
AndrewFG

Replies:

www.cryer.info
Managed Newsgroup Archive