Recent

Author Topic: [SOLVED] Variable Procedure in Thread Class ?  (Read 14914 times)

Fred vS

  • Hero Member
  • *****
  • Posts: 3782
    • StrumPract is the musicians best friend
[SOLVED] Variable Procedure in Thread Class ?
« on: December 05, 2012, 02:31:51 pm »
Hello everybody  :D

I have some trouble with procedure into variable ... :-X

Here the declarations :
Code: [Select]
type
    TMyClass = class(Tobject)
 Public
 ...
    EndProc  : procedure ;
...
  Procedure MyClassProc ;
      end;
 
Procedure    TMyClass.MyClassProc;   
begin
....
EndProc ;
end;     

And here in main form1 :

Code: [Select]
Procedure procclasstest ;
var
testclass : TMyClass;
begin
testclass :=TMyClass.Create;
testclass.EndProc := testendproc;
testclass.MyClassProc ;
end;

Procedure testendproc;
begin
form1.caption := 'It is the End' ;
end;

But if i try to compile :

Quote
testclass.EndProc := testendproc;

Error: Incompatible types: got "untyped" expected "TMyClass.<procedure variable type of procedure;Register>"


What is wrong ?
« Last Edit: December 05, 2012, 11:53: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

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12119
  • Debugger - SynEdit - and more
    • wiki
Re: Variable Procedure in Class ?
« Reply #1 on: December 05, 2012, 02:46:19 pm »
Either
Code: [Select]
{$mode Delphi}
or
Code: [Select]
testclass.EndProc := @testendproc;
Note the "@"

Fred vS

  • Hero Member
  • *****
  • Posts: 3782
    • StrumPract is the musicians best friend
Re: Variable Procedure in Class ?
« Reply #2 on: December 05, 2012, 02:59:21 pm »
@ Martin_Fr
Quote
testclass.EndProc := @testendproc;

Error: Incompatible types: got "<procedure variable type of procedure of object;Register>" expected "TMyClass.<procedure variable type of procedure;Register>"
« Last Edit: December 05, 2012, 03:01:22 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

Fred vS

  • Hero Member
  • *****
  • Posts: 3782
    • StrumPract is the musicians best friend
Re: Variable Procedure in Class ?
« Reply #3 on: December 05, 2012, 03:26:57 pm »
Quote
testclass.EndProc := @testendproc;

Error: Incompatible types: got "<procedure variable type of procedure of object;Register>" expected "TMyClass.<procedure variable type of procedure;Register>"

It is maybe because testendproc must be a procedure declared inside the Class... :-[

If well, how can i access a procedure declared outside the Class ?
« Last Edit: December 05, 2012, 03:35:16 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

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12119
  • Debugger - SynEdit - and more
    • wiki
Re: Variable Procedure in Class ?
« Reply #4 on: December 05, 2012, 04:17:08 pm »
The following works
Code: [Select]
program Project1;
{$mode objfpc}{$H+}

type

  { TMyClass }

  TMyClass = class(Tobject)
  Public
    EndProc  : procedure ;
    Procedure MyClassProc ;
  end;

{ TMyClass }

Procedure testendproc;
begin
  //
end;

procedure TMyClass.MyClassProc;
begin
  EndProc := @testendproc;
end;

begin
end.


You only declared it as "procedure".
Methods of a class a "procedure of object"

Fred vS

  • Hero Member
  • *****
  • Posts: 3782
    • StrumPract is the musicians best friend
Re: Variable Procedure in Class ?
« Reply #5 on: December 05, 2012, 04:23:21 pm »
Quote
procedure TMyClass.MyClassProc;
begin
  EndProc := @testendproc;
end;

Of course, EndProc := @testendproc is done inside the Class (see my earlier post).

But how to do if i want to access a procedure outside the Class ?

How to do EndProc := @testendproc  just after TMyClass was created ?

Otherwise, with your example, i have to change the code of the Class-Object every time i have to load a different procedure.

[Edit] @Parent.testendproc do not work as well...
« Last Edit: December 05, 2012, 04:44:05 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: Variable Procedure in Class ?
« Reply #6 on: December 05, 2012, 04:46:03 pm »
Does this explain it all?
Code: [Select]
type
  TEndProcInside = procedure of object;
  TEndProcOutside = procedure;
 
  TMyClass = class(Tobject)
  Public
    EndProc: TEndProcInside;
    EndProcOut: TEndProcOutside;
  end;

  TUseClass = class(TMyClass)
  Public
    procedure EndProcIn;
    procedure DoCreate;
  end;
...
procedure OutProc;
begin
  // Do something
end;

procedure TUseClass.DoCreate;
begin
  EndProc:=@EndProcIn;
  EndProcOut:=@OutProc;
end;

Then you can test it like:
Code: [Select]
var test: TUseClass;
begin
  test:=TUseClass.Create;
  test.DoCreate;
  test.EndProc;
  test.EndProcOut;
  test.Free;
end;
« Last Edit: December 05, 2012, 04:48:15 pm by User137 »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12119
  • Debugger - SynEdit - and more
    • wiki
Re: Variable Procedure in Class ?
« Reply #7 on: December 05, 2012, 04:57:23 pm »
Post your full source. Your declaration is correct, but you may have other errors.

Maybe something else called "testendproc" too?

Note you define the procedure, after you use it. So there must be a forward declaration?

Fred vS

  • Hero Member
  • *****
  • Posts: 3782
    • StrumPract is the musicians best friend
Re: Variable Procedure in Class ?
« Reply #8 on: December 05, 2012, 05:54:40 pm »
@ User137 thanks. (but i loose my hair and do not help).

Code: [Select]
type
    TMyClass = class(Tobject)
 Public
 ...
    EndProc  :  procedure of object; //<<< the Outside procedure
...
  Procedure MyClassProc ;  //<<< the Inside procedure
      end;
 
Procedure    TMyClass.MyClassProc;   //<<< the Inside procedure
begin
....
EndProc ;  //<<< the Outside procedure
end;     

and

Code: [Select]
Procedure procclasstest ;   
var
testclass : TMyClass;
begin
testclass :=TMyClass.Create;
testclass.EndProc := @testendproc;  //<<< the Outside procedure
testclass.MyClassProc ;   //<<< the Inside procedure
end;

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

Compiles but nothing append on Form1.caption  :'(
« Last Edit: December 05, 2012, 06:13:34 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

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12119
  • Debugger - SynEdit - and more
    • wiki
Re: Variable Procedure in Class ?
« Reply #9 on: December 05, 2012, 06:08:37 pm »
try

EndProc();

the brackets are the dereference operator.

Fred vS

  • Hero Member
  • *****
  • Posts: 3782
    • StrumPract is the musicians best friend
Re: Variable Procedure in Class ?
« Reply #10 on: December 05, 2012, 06:19:36 pm »
Quote
EndProc();

Did not solve  :'(
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

Fred vS

  • Hero Member
  • *****
  • Posts: 3782
    • StrumPract is the musicians best friend
Re: Variable Procedure in Class ?
« Reply #11 on: December 05, 2012, 06:49:14 pm »
OK, i have re-read the code (10.000 lines).

I forgot to notice that TMyClass was class(TThread) so the simplefied code will be :

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

and in Form1 :

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

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

It compiles but EndProc is not executed at end of  testclass.Execute  ...  :'(
« Last Edit: December 05, 2012, 07:02:05 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

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Variable Procedure in Class ?
« Reply #12 on: December 05, 2012, 07:06:22 pm »
Consider what you do.
This:
Code: [Select]
testclass.EndProc := @testendproc;  //<<< the Outside procedure
means that whenever you will call EndProc then code of testendproc will run.
So you have to run
Code: [Select]
EndProc; somewhere.
(Or you can call testendproc; directly.)

Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Fred vS

  • Hero Member
  • *****
  • Posts: 3782
    • StrumPract is the musicians best friend
Re: Variable Procedure in Class ?
« Reply #13 on: December 05, 2012, 07:10:34 pm »
@ Blaazen : not understood, Endproc is run by thread.execute :

Code: [Select]
Procedure    TMyClass.Execute;   //<<< the Inside procedure
begin
....
EndProc ;  //<<< the Outside procedure is executed here
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

Fred vS

  • Hero Member
  • *****
  • Posts: 3782
    • StrumPract is the musicians best friend
Re: Variable Procedure in Thread Class ?
« Reply #14 on: December 05, 2012, 07:27:03 pm »
Try with syncronize, but does not work  :'(

Code: [Select]
Procedure    TMyClass.Execute;   //<<< the Inside procedure
begin
....
Synchronize(EndProc) ;  //<<< the Outside procedure is executed here
end;   
« Last Edit: December 05, 2012, 07:28:58 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

 

TinyPortal © 2005-2018