网友“蓝天白云”在qq群问lazarus多线程的问题,以下代码是“啊D”给出的,但编译出错。
procedure TForm1.Button2Click(Sender: TObject); begin memo1.Text := 'start...'; TThread.CreateAnonymousThread( procedure var i: integer; begin Sleep(1000); for i := 0 to 99999 do TThread.Synchronize(nil, procedure begin Form1.memo1.Text := IntToStr(i); end ); memo1.Text := 'end.'; end ).Start; end;
如需编译上述代码需要fpc 3.3.1及用
{$mode delphi} 或 {$mode objfpc}{$H+} {$ModeSwitch ANONYMOUSFUNCTIONS}
就可以正常编译。
unit Unit1; {$mode objfpc}{$H+} {$ModeSwitch ANONYMOUSFUNCTIONS} interface uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type { TForm1 } TForm1 = class(TForm) Button1: TButton; Button2: TButton; Memo1: TMemo; procedure Button2Click(Sender: TObject); private public end; var Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button2Click(Sender: TObject); begin memo1.Text := 'start...'; TThread.CreateAnonymousThread( procedure var i: integer; begin Sleep(1000); for i := 0 to 99999 do TThread.Synchronize(nil, procedure begin Form1.memo1.Text := IntToStr(i); end ); memo1.Text := 'end.'; end ).Start; end; end.
标签:begin,end,Text,TForm1,用法,lazarus,memo1,多线程,procedure From: https://www.cnblogs.com/qiufeng2014/p/18510802