Recent

Author Topic: [SOLVED] How to talk with parent ?  (Read 17140 times)

Fred vS

  • Hero Member
  • *****
  • Posts: 3922
    • StrumPract is the musicians best friend
[SOLVED] How to talk with parent ?
« on: December 05, 2012, 11:52:53 pm »
Hello everybody.
I have some trouble with calling parent procedure in thread... :-[

That code is working :

Code: [Select]
type
    TMyClass = class(TThread)
 Public
 ...
    EndProc  :  procedure of object; //<<< the Outside procedure is declared inside the Cass.
...
  procedure Execute; override;          //<<< the Inside procedure
      end;
///////////////////////////////////////////////////////// 
Procedure    TMyClass.Execute;   //<<< the Inside procedure
begin
....
EndProc ;  //<<< the Outside procedure
end;     

And in the Form :

Code: [Select]
Procedure procclasstest ; //< the general procedure to test
var
testclass : TMyClass;
begin
testclass :=TMyClass.Create;
testclass.EndProc := @testendproc;  //< the external procedure
testclass.Start ;
end;

Procedure testendproc;  //< the external procedure
begin
form1.caption := 'It is the End' ;
end;


Here code using parent outside procedure EndProc is not executed :

Code: [Select]
type
 TParentClass = class; 
    TChildClass = class(TThread)
   FParent: TParentClass;     
 ...
   procedure Execute; override;          //<<< the Inside procedure
 property Parent: TParentClass read FParent write FParent;     
     end;

type
TParentClass= class(Tobject)
ChildClass               : TChildClass; 
EndProc  :  procedure of object; //< Procedure is declared outside the thread.
end;
 
////////////////////////
Procedure    TChildClass.Execute;   //<<< the Inside procedure
begin
....
Parent.EndProc ;  //<<< the Outside procedure do not run
end;     

and to use in Form :

Code: [Select]
Procedure procclasstest ; //< the general procedure to test
var
testclass : TParentClass;
begin
testclass :=TParentClass.Create;
testclass.EndProc := @testendproc;  //< the external procedure
testclass.ChildClass.Start ;  // start the thread
end;

Procedure testendproc;  //< the external procedure
begin
form1.caption := 'It is the End' ;
end;

Why is it so difficult to deal with parent ?  :-[
« Last Edit: December 06, 2012, 11:47:52 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

mas steindorff

  • Hero Member
  • *****
  • Posts: 583
Re: How to talk with parent ?
« Reply #1 on: December 06, 2012, 02:22:52 am »
I think we need to see a little more code.
where is the create for the childclass (or any of the other Tobjects)?
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

Fred vS

  • Hero Member
  • *****
  • Posts: 3922
    • StrumPract is the musicians best friend
Re: How to talk with parent ?
« Reply #2 on: December 06, 2012, 02:30:08 am »
Code: [Select]
type
 TParentClass = class; 
    TChildClass = class(TThread)
   FParent: TParentClass;     
 ...
   procedure Execute; override;          //<<< the Inside procedure
 constructor Create(CreateSuspended: Boolean; const StackSize: SizeUInt = DefaultStackSize);
  property Parent: TParentClass read FParent write FParent;     
     end;

type
TParentClass= class(Tobject)
ChildClass               : TChildClass; 
EndProc  :  procedure of object; //< Procedure is declared outside the thread.
end;
 
////////////////////////
Procedure    TChildClass.Execute;   //<<< the Inside procedure
begin
....
Parent.EndProc ;  //<<< the Outside procedure do not run
end;     

 constructor TChildClass.Create(CreateSuspended: Boolean;
   const StackSize: SizeUInt);
begin
  inherited Create(CreateSuspended,StackSize);
   FreeOnTerminate := True;
   Parent:=FParent;
  end;   

and in Form :

Code: [Select]
Procedure procclasstest ; //< the general procedure to test
var
testclass : TParentClass;
begin
testclass :=TParentClass.Create;
testclass.EndProc := @testendproc;  //< the external procedure
testclass.ChildClass := TChildClass.Create(true);
testclass.ChildClass.Start ;  // start the thread
end;

Procedure testendproc;  //< the external procedure
begin
form1.caption := 'It is the End' ;
end;

« Last Edit: December 06, 2012, 02:46:35 am by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

mas steindorff

  • Hero Member
  • *****
  • Posts: 583
Re: How to talk with parent ?
« Reply #3 on: December 06, 2012, 02:41:43 am »
I see you have
 testclass :=TParentClass.Create;"  "
that creates memory for a tarentclass type tobject that has a tthread thingy in it but were is the memory for this thingy created?

I would have put it in the TParentClass.Create () something like
  self.childclass = tclildclass.create(...)"
... and any other objects that the tparentclass has



windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

Fred vS

  • Hero Member
  • *****
  • Posts: 3922
    • StrumPract is the musicians best friend
Re: How to talk with parent ?
« Reply #4 on: December 06, 2012, 02:55:51 am »
@  mas steindorff : thanks for answer but did not understood.

Quote
self.childclass = tclildclass.create(...)
?

The code i give is working, the thread is running, only the parent procedure inside execute is not running.

I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

mas steindorff

  • Hero Member
  • *****
  • Posts: 583
Re: How to talk with parent ?
« Reply #5 on: December 06, 2012, 03:09:52 am »
in your original code, the thread object is created directly in the form and is called MyClass

in your second cut, your thread object (now named childclass) does not have a create event.
It's owner (TParentClass) does, but not the thread itself.

for any class that has other objects & classes inside it, it needs to ceate and free them the same as if the objects were used outside of class. 
for example: I sometimes need to use a tstringlist inside a object I'm creating and am required to include a myobject.create that has a tstr := tsringlist.create and a myobject.destroy() that includes a tstr.free statement for proper memory management
« Last Edit: December 06, 2012, 03:14:17 am by mas steindorff »
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: How to talk with parent ?
« Reply #6 on: December 06, 2012, 09:36:21 am »
Code: [Select]
Public
 ...
    EndProc  :  procedure of object

Code: [Select]
Procedure testendproc;  //< the external procedure
begin
form1.caption := 'It is the End' ;
end;

Code: [Select]
testclass.EndProc := @testendproc;
You have procedure " of object", yet you also have procedure testendproc that doesn't belong to any class. I don't understand how your code even compiles.

I guess you should also replace this:
Code: [Select]
testclass.EndProc := @testendproc;with calling this after childclass is created:
Code: [Select]
testclass.childclass.EndProc := @testendproc;
Also, you don't really need 2 classes if you just want to use TThread. Just 1 class that does all that would be simpler, but i don't know deeper needs for your program  ;)
« Last Edit: December 06, 2012, 09:48:35 am by User137 »

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: How to talk with parent ?
« Reply #7 on: December 06, 2012, 09:50:40 am »
To be honest, that's another reason for asking people to submit compilable example project instead of just snippets.

A lot of people seem to take the lazier approach of copy and paste and then change parts of their production code or perhaps various versions of their example project, leading to a lot of unnecessary confusion.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Fred vS

  • Hero Member
  • *****
  • Posts: 3922
    • StrumPract is the musicians best friend
Re: How to talk with parent ?
« Reply #8 on: December 06, 2012, 02:24:46 pm »
Hello everybody and thanks to take care about that difficult thing to explain.

Here complete code and in attachments the test project (working).
There are 2 buttons:
Button 1 loads a normal alone thread and run a external procedure (works perfect).
Button 2 loads a child thread and run a external procedure (do not works).

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, Forms, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
      Procedure testendproc;
    private
    { private declarations }
  public

    { public declarations }
  end;

  type
    TMyClass = class(TThread)
 Public
    EndProc  :  procedure of object; //<<< the Outside procedure is declared inside the Cass.
   constructor Create(CreateSuspended: Boolean; const StackSize: SizeUInt = DefaultStackSize);
    procedure Execute; override;          //<<< the Inside procedure
      end;

 type
 TParentClass = class;
    TChildClass = class(TThread)
   FParent: TParentClass;
   procedure Execute; override;          //<<< the Inside procedure
 constructor Create(CreateSuspended: Boolean; const StackSize: SizeUInt = DefaultStackSize);
  property Parent: TParentClass read FParent write FParent;
     end;

type
TParentClass= class(Tobject)
ChildClass               : TChildClass;
EndProc  :  procedure of object; //< Procedure is declared outside the thread.
end;

var
  Form1: TForm1;


implementation

{$R *.lfm}

{ TForm1 }

Procedure TForm1.testendproc;  //< the external procedure
begin
form1.label1.caption := 'External Procedure executed' ;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
testclass : TMyClass;
begin
label1.caption := 'Init of Thread...' ;
testclass :=TMyClass.Create(true);
testclass.EndProc := @testendproc;  //< the external procedure
testclass.Start ;  // start the thread

end;

procedure TForm1.Button2Click(Sender: TObject);
var
testclass : TParentClass;
begin
   label1.caption := 'Init of Thread...' ;
testclass :=TParentClass.Create;
testclass.EndProc := @testendproc;  //< the external procedure
testclass.ChildClass := TChildClass.Create(true);
testclass.ChildClass.Start ;  // start the thread
end;

Procedure    TMyClass.Execute;   //<<< the Inside procedure
begin
EndProc ;  //<<< the Outside procedure runs
end;

 constructor TMyClass.Create(CreateSuspended: Boolean;
   const StackSize: SizeUInt);
begin
  inherited Create(CreateSuspended,StackSize);
   FreeOnTerminate := True;
    end ;

 Procedure    TChildClass.Execute;   //<<< the Inside procedure
 begin
 Parent.EndProc ;  //<<< the Outside procedure do not run
 end;

  constructor TChildClass.Create(CreateSuspended: Boolean;
    const StackSize: SizeUInt);
 begin
   inherited Create(CreateSuspended,StackSize);
    FreeOnTerminate := True;
    Parent:=FParent;
   end;

 end.
     


« Last Edit: December 06, 2012, 05:03:13 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: How to talk with parent ?
« Reply #9 on: December 06, 2012, 04:22:01 pm »
Code: [Select]
procedure TForm1.Button2Click(Sender: TObject);
var
  testclass : TParentClass;
begin
  label1.caption := 'Init of Thread...' ;
  testclass :=TParentClass.Create;
  testclass.EndProc := @testendproc;  //< the external procedure
  testclass.ChildClass := TChildClass.Create(true);
  testclass.ChildClass.Parent:=testclass; // You hadn't given parent any value
  testclass.ChildClass.Start ;  // start the thread
end;

Fred vS

  • Hero Member
  • *****
  • Posts: 3922
    • StrumPract is the musicians best friend
Re: How to talk with parent ?
« Reply #10 on: December 06, 2012, 04:54:58 pm »
@ User137 : Hourrra, Youpie, Iiiiiiiaaaaa, you get it  ;D

Many, many, many thanks.

Quote
testclass.ChildClass.Parent:=testclass; // You hadn't given parent any value
does the trick.

Well, Class and OOP become every day (and night) more sexy.
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: [SOLVED] How to talk with parent ?
« Reply #11 on: December 06, 2012, 05:53:11 pm »
@Fred: glad it works! I think you're now the perfect test person to see if this article:
http://wiki.lazarus.freepascal.org/property#Objects_as_properties
makes sense...

If you'd like to, please correct/update the article, or let me know in this thread if something is unclear/missing & I'll try and correct it.

Thanks,
BigChimp
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Fred vS

  • Hero Member
  • *****
  • Posts: 3922
    • StrumPract is the musicians best friend
Re: [SOLVED] How to talk with parent ?
« Reply #12 on: December 06, 2012, 07:25:48 pm »
@ BigChimp : With the greatest pleasure.  ::)

But, before to go, my brain-debugger have already a problem with :
Quote
  MyCar.Color:='Green'; 

I do not see any declaration of TCar.Color (only TCar.FColor)  :-X
(It is maybe a convention to add "F" before variable and i did not know that  :-[ )

Code: [Select]
type
  TCar = class
  private
    FColor: string;
    FBuildYear: integer;
    procedure SetColor(CarColor: string);
  public
    property TheColor: string read FColor write SetColor; //Reads directly from the FColor variable;
    // directs writes to the SetColor procedure that then changes the FColor variable..
 
  end;
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: [SOLVED] How to talk with parent ?
« Reply #13 on: December 06, 2012, 07:30:06 pm »
You're completely right - the example code is wrong - it should have been like this:
Code: [Select]
type
  TCar = class
  private
    FColor: string;
    FBuildYear: integer;
    procedure SetColor(CarColor: string);
  public
    property Color: string read FColor write SetColor; //Color, not TheColor!!!
 
  end;
Edit:
And yes, F<variable name> is a naming convention for class-level variables. If you use it you can easily distinguish class-level variables from variables that are local in your class procedures/functions.
FColor would not be accessible from outside the class because it was declared in the private section: only available within the TCar class code itself.

Updated the example code
« Last Edit: December 06, 2012, 07:35:31 pm by BigChimp »
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Fred vS

  • Hero Member
  • *****
  • Posts: 3922
    • StrumPract is the musicians best friend
Re: [SOLVED] How to talk with parent ?
« Reply #14 on: December 06, 2012, 07:43:35 pm »
@ BigChimp : Ouf, im not completely crazy  :-X

OK, i read next line, see you soon...
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

 

TinyPortal © 2005-2018