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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thankx, great code!
i just added the ics http component.. why OnRequestDone in the PrimeAdder Thread doesn't work?
(TPrimeAdder.AHttpCliOnReq uestDone;)
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(Send er: 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(form at('Added request for %s',[AHttpCli.URL]));
_sub.Resume;
while not FDoneSubThread or not Terminated do begin // wait
Sleep(100);
end;
end;
procedure TPrimeAdder.AHttpCliOnRequ estDone;
var
AHttpCli : THttpCli;
begin
AHttpCli := Sender as THttpCli;
MyRecord.site := AHttpCLi.URL;
MyRecord.statuscode := AHttpCli.StatusCode;
Form1.Memo1.Lines.Add(form at('done %d',[MyRecord.site]));
end;
procedure TSubThread.Execute;
begin
FreeOnTerminate := true;
// realization
end;
constructor TSubThread.Create(CreateSu spended: boolean; AOwnerThread: TPrimeAdder);
begin
FOwnerThread := AOwnerThread;
inherited Create(CreateSuspended);
end;
destructor TSubThread.Destroy;
begin
FOwnerThread.FDoneSubThrea d := 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?
i just added the ics http component.. why OnRequestDone in the PrimeAdder Thread doesn't work?
(TPrimeAdder.AHttpCliOnReq
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(Send
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(form
_sub.Resume;
while not FDoneSubThread or not Terminated do begin // wait
Sleep(100);
end;
end;
procedure TPrimeAdder.AHttpCliOnRequ
var
AHttpCli : THttpCli;
begin
AHttpCli := Sender as THttpCli;
MyRecord.site := AHttpCLi.URL;
MyRecord.statuscode := AHttpCli.StatusCode;
Form1.Memo1.Lines.Add(form
end;
procedure TSubThread.Execute;
begin
FreeOnTerminate := true;
// realization
end;
constructor TSubThread.Create(CreateSu
begin
FOwnerThread := AOwnerThread;
inherited Create(CreateSuspended);
end;
destructor TSubThread.Destroy;
begin
FOwnerThread.FDoneSubThrea
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
begin
StartThreads;
end;
end.
can you help me?
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;