Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2013-04

How to use TThread?

6 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

silvioprog

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
Antonio Fortuny
to silvioprog
First of all change the order of lines 25-26
     inherited Create(False);
     FreeOnTerminate := True;
instead of
     FreeOnTerminate := True;
     inherited Create(False);

The, drop the DoExecute procedure and replace it by the Execute procedure. It is useless to Synchronize a thread's procedure inside itself ! After the creation, the thread executes immediately the overriden procedure Execute because, at creation time (inherited Create(False); ) the thread is created in the state NON SUSPENDED. The use of the DoExecute is obviously useless
our program will execute the procedure Excute only once as there is no loop.

Sita Software
*Antonio Fortuny*
Senior Software engineer

220, avenue de la Liberté
L-4602 Niederkorn
Tel.: +352 58 00 93 - 93
www.sitasoftware.lu <http://www.sitasoftware.lu>
Your IT Partner

silvioprog wrote 15 lines
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> _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Jeppe Græsdal Johansen
to silvioprog: I tried to use TThread in a small sample, but it don't works. The code is very simple,…
silvioprog wrote 11 lines
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.
Michael Van Canneyt
to Jeppe Græsdal Johansen
Jeppe Græsdal Johansen wrote 12 lines
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.

That bug has been fixed long time ago.

Michael.

silvioprog
to Antonio Fortuny: First of all change the order of lines 25-26 inherited Create(False); FreeOnTerminate :=…

2013/4/18 Antonio Fortuny

Show quoted text 28 lines
First of all change the order of lines 25-26 inherited Create(False); FreeOnTerminate := True; instead of FreeOnTerminate := True; inherited Create(False); The, drop the DoExecute procedure and replace it by the Execute procedure. It is useless to Synchronize a thread's procedure inside itself ! After the creation, the thread executes immediately the overriden procedure Execute because, at creation time (inherited Create(False); ) the thread is created in the state NON SUSPENDED. The use of the DoExecute is obviously useless our program will execute the procedure Excute only once as there is no loop. [image: Sita Software] *Antonio Fortuny* Senior Software engineer 220, avenue de la Liberté L-4602 Niederkorn Tel.: +352 58 00 93 - 93 www.sitasoftware.lu [image: Your IT Partner]

Thank for the explanation. ;)

Silvio Clécio My public projects - github.com/silvioprog
silvioprog
to Jeppe Græsdal Johansen: Here's a version which I think should work everywhere. I think there might be a bug in…

2013/4/18 Jeppe Græsdal Johansen

Show quoted text 70 lines
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