Newsgroups : Borland : borland.public.delphi.internet.winsock : 2005 Jul : Disconnect TIdHttp in thread

www.cryer.info
Managed Newsgroup Archive

Disconnect TIdHttp in thread

Subject:Disconnect TIdHttp in thread
Posted by:"Karsten" (..@mail.com)
Date:Tue, 26 Jul 2005 12:32:13

Hi

I have a thread where I use HTTP.Get to download a file. Is there a good
threadsafe way to disconnect the HTTP while it connects to the host.

The best I can figure out is something like this

But I think there must be a better way


private
  InCS: Boolean;


destructor thread.destroy;
begin
    EnterCriticalSection(HTTPCS);
    try
        if Assigned(HTTP) then
            HTTP.Disconnect;
    finally
        LeaveCriticalSection(HTTPCS);
    end;
    inherited
end;

procedure thread.execute;
begin
  try
        EnterCriticalSection(HTTPCS);
        try
            HTTP := TIdHTTP.Create(nil);
            HTTP.OnStatus := OnHTTPStatus;
        finally
            if InCS then
            begin
              InCS := False;
              LeaveCriticalSection(HTTPCS);
            end;
        end;
  except
        on E: EidException do
        begin
          ....
        end;
  end;
end;

procedure TDownloadThread.OnHTTPStatus(ASender: TObject; const AStatus:
TIdStatus; const AStatusText: string);
begin
    if AStatus in [hsResolving, hsConnecting] and InCS then
    begin
        InCS := False;
        LeaveCriticalSection(HTTPCS);
    end;
end;

Replies:

www.cryer.info
Managed Newsgroup Archive