TThread - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

TThread

First here is the unit with the thread

unit Unit2;
interface
uses
Classes,windows;
type
dirmon = class(TThread)
public
nr:integer;
constructor create(a:integer);
private
{ Private declarations }
protected
procedure Execute; override;
end;
implementation
{ dirmon }
constructor dirmon.create(a:integer);
begin
nr:=a;
FreeOnTerminate:=true;
inherited Create(False);
end;

procedure dirmon.Execute;
var ok:boolean;s:string;i:integer;
begin
ok:=false;
for i:=2 to trunc(sqrt(nr))do
if nr mod i=0 then ok:=true;
if ok then s:='nu este prim'
else s:='este prim';
messagebox(0,pchar(s),nil,0);
inherited Terminate;
{ Place thread code here }
end;
end.

It tests if a number is prime or not and pops up a dialogbox.It compiles but when I call the create constructor during running it gives an access violation error and delphi points at the first line in the create procedure:nr:=a;

Here is the implememtantion of the unit1 which calls the create constr

implementation
uses unit2;
{$R *.dfm}
var ex:dirmon;
procedure TForm1.Button1Click(Sender: TObject);
begin
ex.create(strtoint(edit1.Text));
end;

end.

Can you tell me what is the problem ?
10x

Comments

  • zibadianzibadian Posts: 6,349Member
    : First here is the unit with the thread
    : [code]
    : unit Unit2;
    : interface
    : uses
    : Classes,windows;
    : type
    : dirmon = class(TThread)
    : public
    : nr:integer;
    : constructor create(a:integer);
    : private
    : { Private declarations }
    : protected
    : procedure Execute; override;
    : end;
    : implementation
    : { dirmon }
    : constructor dirmon.create(a:integer);
    : begin
    : nr:=a;
    : FreeOnTerminate:=true;
    : inherited Create(False);
    : end;
    :
    : procedure dirmon.Execute;
    : var ok:boolean;s:string;i:integer;
    : begin
    : ok:=false;
    : for i:=2 to trunc(sqrt(nr))do
    : if nr mod i=0 then ok:=true;
    : if ok then s:='nu este prim'
    : else s:='este prim';
    : messagebox(0,pchar(s),nil,0);
    : inherited Terminate;
    : { Place thread code here }
    : end;
    : end.
    :
    : It tests if a number is prime or not and pops up a dialogbox.It compiles but when I call the create constructor during running it gives an access violation error and delphi points at the first line in the create procedure:nr:=a;
    :
    : Here is the implememtantion of the unit1 which calls the create constr
    :
    : implementation
    : uses unit2;
    : {$R *.dfm}
    : var ex:dirmon;
    : procedure TForm1.Button1Click(Sender: TObject);
    : begin
    : [red]ex.create(strtoint(edit1.Text));[/red]
    : end;
    :
    : end.
    : [/code]
    : Can you tell me what is the problem ?
    : 10x
    :
    The read line is the problem. The proper way to create an object is this:
    [code]
    ex := DirMon.Create(strtoint(edit1.Text));
    [/code]
    This is because Delphi needs to know which type the object is. The following code is also valid:
    [code]
    var
    [b]ex: TThread[/b];

    procedure TForm1.Button1Click(Sender: TObject);
    begin
    ex := DirMon.Create(strtoint(edit1.Text));
    [/code]

  • bogdanul2003bogdanul2003 Posts: 153Member
    [b][red]This message was edited by bogdanul2003 at 2005-9-4 11:58:45[/red][/b][hr]
    : : First here is the unit with the thread
    : : [code]
    : : unit Unit2;
    : : interface
    : : uses
    : : Classes,windows;
    : : type
    : : dirmon = class(TThread)
    : : public
    : : nr:integer;
    : : constructor create(a:integer);
    : : private
    : : { Private declarations }
    : : protected
    : : procedure Execute; override;
    : : end;
    : : implementation
    : : { dirmon }
    : : constructor dirmon.create(a:integer);
    : : begin
    : : nr:=a;
    : : FreeOnTerminate:=true;
    : : inherited Create(False);
    : : end;
    : :
    : : procedure dirmon.Execute;
    : : var ok:boolean;s:string;i:integer;
    : : begin
    : : ok:=false;
    : : for i:=2 to trunc(sqrt(nr))do
    : : if nr mod i=0 then ok:=true;
    : : if ok then s:='nu este prim'
    : : else s:='este prim';
    : : messagebox(0,pchar(s),nil,0);
    : : inherited Terminate;
    : : { Place thread code here }
    : : end;
    : : end.
    : :
    : : It tests if a number is prime or not and pops up a dialogbox.It compiles but when I call the create constructor during running it gives an access violation error and delphi points at the first line in the create procedure:nr:=a;
    : :
    : : Here is the implememtantion of the unit1 which calls the create constr
    : :
    : : implementation
    : : uses unit2;
    : : {$R *.dfm}
    : : var ex:dirmon;
    : : procedure TForm1.Button1Click(Sender: TObject);
    : : begin
    : : [red]ex.create(strtoint(edit1.Text));[/red]
    : : end;
    : :
    : : end.
    : : [/code]
    : : Can you tell me what is the problem ?
    : : 10x
    : :
    : The read line is the problem. The proper way to create an object is this:
    : [code]
    : ex := DirMon.Create(strtoint(edit1.Text));
    : [/code]
    : This is because Delphi needs to know which type the object is. The following code is also valid:
    : [code]
    : var
    : [b]ex: TThread[/b];
    :
    : procedure TForm1.Button1Click(Sender: TObject);
    : begin
    : ex := DirMon.Create(strtoint(edit1.Text));
    : [/code]
    :
    :

    10x zibadian


Sign In or Register to comment.