Recent

Author Topic: Identifier not found  (Read 640 times)

jan741

  • New Member
  • *
  • Posts: 15
Identifier not found
« on: December 09, 2022, 05:16:53 pm »
Identifier deproc not found on function-call(see comment in code below). Exactly based on this example. https://wiki.lazarus.freepascal.org/Asynchronous_Calls

What's going wrong?

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs,
  9.   StdCtrls, dbugintf, LazLoggerBase, pingsend;
  10.  
  11. const
  12.   threadcount = 100;
  13.   stringlen = 10000;
  14.  
  15. type
  16.  
  17.   { TForm1 }
  18.  
  19.   TForm1 = class(TForm)
  20.     Button1: TButton;
  21.     Button2: TButton;
  22.     procedure Button1Click(Sender: TObject);
  23.     procedure Button2Click(Sender: TObject);
  24.  
  25.   private
  26.     { private declarations }
  27.     procedure deproc(input: PtrInt);
  28.   public
  29.     { public declarations }
  30.   end;
  31.  
  32.   myrec = record
  33.     i : longint;
  34.     s : string;
  35.   end;
  36.  
  37.   Pmyrec = ^myrec;
  38.  
  39. var
  40.   Form1: TForm1;
  41.   finished : longint;
  42.   y : longint;
  43.   nr : integer;
  44. threadvar
  45.   thri : ptrint;
  46.  
  47. implementation
  48. {$R *.lfm}
  49.  
  50. { TForm1 }
  51.  
  52.  
  53. function f(p : Pointer) : ptrint;
  54.  
  55. var
  56.   s : ansistring;
  57.   thisrec : myrec;
  58. begin
  59.   thisrec := Pmyrec(p)^;
  60.   Writeln('thread ', longint(thisrec.i), ' started');
  61.   thri:=0;
  62.   while (thri<stringlen) do
  63.     begin
  64.     s:=s+'1';
  65.     inc(thri);
  66.     end;
  67.   Writeln('thread ',thisrec.i,' finished with',thisrec.s);
  68.   InterLockedIncrement(finished);
  69.   f:=0;
  70.   nr:=10;
  71.   Application.QueueAsyncCall(@deproc, nr);  //<--------------------------------------------------------------------------------------- error happening here
  72. end;
  73.  
  74. procedure TForm1.Button1Click(Sender: TObject);
  75. begin
  76.   y:= 1;
  77. end;
  78.  
  79. procedure TForm1.Button2Click(Sender: TObject);
  80. var
  81.   r: array[1..threadcount] of myrec;
  82.   pr: ^myrec;
  83.   x : longint;
  84. begin
  85.    finished:=0;
  86.  
  87.    for x :=1 to threadcount do
  88.      begin
  89.        r[x].s := ' test ';
  90.        r[x].i:=x;
  91.        BeginThread(@f,@r[x]);
  92.        writeln('ready for next');
  93.      end;
  94.    while finished<threadcount do ;
  95. end;
  96.  
  97. procedure deproc(input:PtrInt);
  98. begin
  99.    writeln('async called.');
  100. end;
  101.  
  102. end.    
« Last Edit: December 09, 2022, 05:21:20 pm by jan741 »

Handoko

  • Hero Member
  • *****
  • Posts: 5149
  • My goal: build my own game engine using Lazarus
Re: Identifier not found
« Reply #1 on: December 09, 2022, 05:51:50 pm »
That line #97, should be:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.deproc(input: PtrInt);

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Identifier not found
« Reply #2 on: December 09, 2022, 05:53:15 pm »
Try this adaptation:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$IFDEF MSWINDOWS} {$apptype console} {$EndIf}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  10.  
  11. const
  12.     threadcount = 100;
  13.     stringlen = 10000;
  14.  
  15. type
  16.  
  17.   TForm1 = class(TForm)
  18.     Button2: TButton;
  19.     procedure Button2Click(Sender: TObject);
  20.   private
  21.     procedure deproc(input: PtrInt);
  22.   end;
  23.  
  24.  
  25.   myrec = record
  26.       i : longint;
  27.       s : string;
  28.   end;
  29.   Pmyrec = ^myrec;
  30.  
  31. var
  32.   Form1: TForm1;
  33.   finished, nr: LongInt;
  34.  
  35. threadvar
  36.   thri : PtrInt;
  37.  
  38. implementation
  39.  
  40. {$R *.lfm}
  41.  
  42. function f(p: Pointer): PtrInt;
  43. var
  44.   s: AnsiString = '';
  45.   thisrec: myrec;
  46. begin
  47.   thisrec := Pmyrec(p)^;
  48.   Writeln('thread ', thisrec.i, ' started');
  49.   thri := 0;
  50.   while (thri<stringlen) do
  51.     begin
  52.     s := s+'1';
  53.     Inc(thri);
  54.     end;
  55.   Writeln('thread ',thisrec.i,' finished with',thisrec.s);
  56.   InterLockedIncrement(finished);
  57.   f:=0;
  58.   nr:=10;
  59.   Application.QueueAsyncCall(@Form1.deproc, nr); // <--------------- error happening here
  60. end;
  61.  
  62. procedure TForm1.Button2Click(Sender: TObject);
  63. var
  64.   r: array[1..threadcount] of myrec;
  65.   pr: ^myrec;
  66.   x : longint;
  67. begin
  68.    finished:=0;
  69.    for x :=1 to threadcount do
  70.      begin
  71.        r[x].s := ' test ';
  72.        r[x].i:=x;
  73.        BeginThread(@f, @r[x]);
  74.        writeln('ready for next');
  75.      end;
  76.    while finished<threadcount do ;
  77. end;
  78.  
  79. procedure TForm1.deproc(input: PtrInt);
  80. begin
  81.   WriteLn('async called.');
  82. end;
  83.  
  84. end.
« Last Edit: December 09, 2022, 06:44:41 pm by howardpc »

Thaddy

  • Hero Member
  • *****
  • Posts: 14359
  • Sensorship about opinions does not belong here.
Re: Identifier not found
« Reply #3 on: December 09, 2022, 06:16:22 pm »
The error may be here: longint(thisrec.i). That should be ptrUint not ptrInt. Pointers can not be negative and longint does not work above 32 bit.
« Last Edit: December 09, 2022, 06:18:04 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Identifier not found
« Reply #4 on: December 09, 2022, 06:46:12 pm »
@Thaddy
Actually the Longint() cast is not needed at all, since the record field is already a longint.
He is not casting a pointer at that point in the code.
I've changed my example accordingly.

jan741

  • New Member
  • *
  • Posts: 15
Re: Identifier not found
« Reply #5 on: December 10, 2022, 12:00:04 am »
It works now. Thanks.  8)

 

TinyPortal © 2005-2018