Recent

Author Topic: TThread.CreateAnonymousThread  (Read 414 times)

Pe3s

  • Hero Member
  • *****
  • Posts: 573
TThread.CreateAnonymousThread
« on: November 28, 2024, 09:48:51 pm »
Hello, for using TThread.CreateAnonymousThread thread
Is there any configuration required.
How can I improve the code ?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   TThread.CreateAnonymousThread(
  4.   procedure
  5.   begin
  6.      // code
  7.   end
  8.   ).Start;
  9. end;        
  10.  

Code: Pascal  [Select][+][-]
  1. unit1.pas(325,3) Error: Illegal expression
  2. unit1.pas(326,3) Error: Syntax error, ")" expected but "BEGIN" found
  3.  
« Last Edit: November 28, 2024, 10:13:37 pm by Pe3s »

korba812

  • Sr. Member
  • ****
  • Posts: 442
Re: TThread.CreateAnonymousThread
« Reply #1 on: November 28, 2024, 11:26:58 pm »
Probably anonymous functions should be enabled by modeswitch. Try putting {$modeswitch ANONYMOUSFUNCTIONS} in your unit.

vercetti

  • New Member
  • *
  • Posts: 10
Re: TThread.CreateAnonymousThread
« Reply #2 on: November 29, 2024, 09:12:33 am »
Quote
{$modeswitch ANONYMOUSFUNCTIONS}

From which version did this become available ?

I use: Lazarus 3.4 and FPC 3.2.2 and get a warning:

Quote
Warning: Illegal compiler switch "ANONYMOUSFUNCTIONS+"

Thaddy

  • Hero Member
  • *****
  • Posts: 16187
  • Censorship about opinions does not belong here.
Re: TThread.CreateAnonymousThread
« Reply #3 on: November 29, 2024, 09:31:12 am »
It is only in trunk/main.
Better than createanonymousthread is simply executeinthread.
I gave an example some weeks ago:
https://forum.lazarus.freepascal.org/index.php/topic,68823.msg532746.html#msg532746

And can be simplified further.
In 3.2.x this is only possible by taking a function pointer instead.
« Last Edit: November 29, 2024, 10:34:24 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

 

TinyPortal © 2005-2018