[fpc-pascal] How to use TThread?

Jeppe Græsdal Johansen jeppe at j-software.dk
Thu Apr 18 09:05:15 CEST 2013


Den 18-04-2013 07:03, silvioprog skrev:
> Hi,
>
> I tried to use TThread in a small sample, but it don't works. The code 
> is very simple, and I'm sending it in attached.
>
> Thanks,
>
> -- 
> Silvio Clécio
> My public projects - github.com/silvioprog <http://github.com/silvioprog>
>
Here's a version which I think should work everywhere. I think there 
might be a bug in the fphttpclient code. If you don't pass the / at the 
end of the URI it will crash inside the getserverurl function.

program project1;

{$mode objfpc}{$H+}

uses
   {$IFDEF UNIX}{$IFDEF UseCThreads}
   cthreads,
   {$ENDIF}{$ENDIF}
   Classes,
   FPHTTPClient;

type

   { THTTPClientThread }

   THTTPClientThread = class(TThread)
   protected
     procedure Execute; override;
   public
     constructor Create;
   end;

   { THTTPClientThread }

   constructor THTTPClientThread.Create;
   begin
     inherited Create(False);
     FreeOnTerminate := True;
   end;

   procedure THTTPClientThread.Execute;
   var
     VResult: TStringList;
     VHttp: TFPHTTPClient;
   begin
     VHttp := TFPHTTPClient.Create(nil);
     VResult := TStringList.Create;
     try
       VHttp.Get('http://silvioprog.com.br/', VResult);
       VResult.SaveToFile('OUT.TXT')
     finally
       VResult.Free;
       VHttp.Free;
     end;
   end;

var Thread: THTTPClientThread;
begin
   Thread := THTTPClientThread.Create;
   Thread.WaitFor;
end.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20130418/d7be3fe3/attachment.html>


More information about the fpc-pascal mailing list