Link to home
Start Free TrialLog in
Avatar of St4rF0rce
St4rF0rce

asked on

Working with Threads

hi,  
how can i allow my app to support multi-threading?
 i want to make an app that supports like 50threads, and that each thread have a subthread, so that the first thread must wait until the subthread terminate.

like..

type
  TPrimeAdder = class(TThread)
  private
  fPosition, fPosition2: integer;
  protected
    procedure ONE;
    procedure TWO;
    procedure Execute; override;
    procedure UpdateProgress;
  public

  end;

procedure TForm1.StartThread;
var
Thread1: TPrimeAdder;
begin

Thread1 := TPrimeAdder.Create (TRUE);
Thread1.FreeOnTerminate := TRUE;
Thread1.Execute;
end;


in this way i just get one thread.. How can i Duplicate it?
Avatar of Kyle Foster
Kyle Foster
Flag of United States of America image

The following code will start 50 threads.  but you don't have a reference back to the thread this way.
I prefer to add the threads to a TList and free them myself when finished.  
The function below starts the thread and then forgets about it while it is doint it's thing.

procedure TForm1.StartThreads;
var
   Thread1: TPrimeAdder;
   ndx : Integer;
begin
  for ndx := 1 to 50 do begin
     Thread1 := TPrimeAdder.Create (TRUE);
     Thread1.FreeOnTerminate := TRUE;
     Thread1.Execute;
  end;
end;

ASKER CERTIFIED SOLUTION
Avatar of sas13
sas13
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of St4rF0rce
St4rF0rce

ASKER

Thankx, great code!

i just added the ics http component.. why OnRequestDone in the PrimeAdder Thread doesn't work?
(TPrimeAdder.AHttpCliOnRequestDone;)
My idea is to use the first thread to connect to a specific url using the Ics component, and then use the sub one to get another url.

here's my Test unit..


unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, HttpProt;

type
 TRecord = Record
 site: String;
 statuscode:integer;
 end;

type
  TPrimeAdder = class(TThread)
  private
    FDoneSubThread: boolean;
  protected
    procedure Execute; override;
    procedure AHttpCliOnRequestDone(Sender: TObject; RqType: THttpRequest;
    Error: Word);
  end;

  TSubThread = class(TThread)
   private
     FOwnerThread: TPrimeAdder;
   protected
     procedure Execute; override;
   public
     constructor Create(CreateSuspended: boolean; AOwnerThread: TPrimeAdder); reintroduce;
     destructor Destroy; override;
end;


type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Memo1: TMemo;
    Button1: TButton;
    procedure StartThreads;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
  end;


var
  Form1: TForm1;
  ABORT: Boolean;
  MyRecord : TRecord;

implementation

{$R *.dfm}

procedure TPrimeAdder.Execute;
var
  _sub: TSubThread;
  AHttpCli: THttpCli;
begin
  FreeOnTerminate := true;
  FDoneSubThread := false;
  _sub := TSubThread.Create(true, self);

  AHttpCli := THttpCli.Create(nil);
  AHttpCli.URL := 'http://www.google.com';
  AHttpCli.OnRequestDone :=  AHttpCliOnRequestDone;
  AHttpCLi.GetASync;
  Form1.Memo1.Lines.Add(format('Added request for %s',[AHttpCli.URL]));

  _sub.Resume;
  while not FDoneSubThread or not Terminated do begin // wait
   Sleep(100);
  end;
end;

procedure TPrimeAdder.AHttpCliOnRequestDone;
var
AHttpCli : THttpCli;
begin
  AHttpCli := Sender as THttpCli;
  MyRecord.site := AHttpCLi.URL;
  MyRecord.statuscode := AHttpCli.StatusCode;
  Form1.Memo1.Lines.Add(format('done %d',[MyRecord.site]));

end;

procedure TSubThread.Execute;
begin
  FreeOnTerminate := true;
  // realization
end;

constructor TSubThread.Create(CreateSuspended: boolean; AOwnerThread: TPrimeAdder);
begin
  FOwnerThread := AOwnerThread;
  inherited Create(CreateSuspended);
end;

destructor TSubThread.Destroy;
begin
  FOwnerThread.FDoneSubThread := true;
  inherited Destroy;
end;

procedure TForm1.StartThreads;
var
   ndx : Integer;
begin
  for ndx := 1 to StrToInt(Edit1.Text) do
     TPrimeAdder.Create (false);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
StartThreads;
end;

end.




can you help me?