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?
St4rF0rceAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle FosterCEOCommented:
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;

sas13Commented:
type
  TPrimeAdder = class(TThread)
  private
    FDoneSubThread: boolean;
  protected
    procedure Execute; override;
  end;
 
  TSubThread = class(TThread)
   private
     FOwnerThread: TPrimeAdder;
   protected
     procedure Execute; override;
   public
     constructor Create(CreateSuspended: boolean; AOwnerThread: TPrimeAdder); reintroduce;  
     destructor Destroy; override;
   end;
 
procedure TPrimeAdder.Execute;
var
  _sub: TSubThread;
begin
  FreeOnTerminate := true;
  FDoneSubThread := false;
  _sub := TSubThread.Create(true, self);
  //...
  _sub.Resume;
  while not FDoneSubThread or not Terminated do // wait
   Sleep(100);
end;

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

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

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


// and start
procedure TForm1.StartThreads;
var
   ndx : Integer;
begin
  for ndx := 1 to 50 do
     TPrimeAdder.Create (false);
end
 


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
St4rF0rceAuthor Commented:
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?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.