[fpc-pascal] How to use TThread?

silvioprog silvioprog at gmail.com
Thu Apr 18 19:48:54 CEST 2013


2013/4/18 Jeppe Græsdal Johansen <jeppe at j-software.dk>

>  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
>
>  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.
>

Worked fine, thank you! :)

-- 
Silvio Clécio
My public projects - github.com/silvioprog
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20130418/2651a194/attachment.html>


More information about the fpc-pascal mailing list