lazarus free pascal不支持匿名方法

时间:2021-01-26
本文章向大家介绍lazarus free pascal不支持匿名方法,主要包括lazarus free pascal不支持匿名方法使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Unit2;
type
  TMyProc = procedure of object;

  { TForm1 }

  TForm1 = class(TForm)
    CheckBox1: TCheckBox;
    Frame1_1: TFrame1;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
  private
    procedure DoWork;
  public
    procedure Exec;
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure UpdateTime;
begin
  while not Form1.CheckBox1.Checked do
  begin
    TThread.Synchronize(nil, @Form1.Exec);
    TThread.Sleep(500);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
type
  PThreadMethod = ^TThreadMethod;

var
  Method: TMethod;
  Proc: TThreadMethod;

  procedure NestedProc;
  begin
    while not Form1.CheckBox1.Checked do
    begin
      TThread.Synchronize(nil, @Exec);
      TThread.Sleep(500);
    end;
  end;

begin
  Method.Data := @Self;
  Method.Code := @NestedProc;  //可以
  Method.Code := @UpdateTime;  //可以
  Proc := PThreadMethod(@Method)^;
  TThread.ExecuteInThread(Proc);
end;

procedure TForm1.DoWork;
begin
  while not CheckBox1.Checked do
  begin
    TThread.Synchronize(nil, @Exec);
    TThread.Sleep(500);
  end;
end;

procedure TForm1.Exec;
begin
  Label1.Caption := DateTimeToStr(Now);
end;

end.

原文地址:https://www.cnblogs.com/Jiaojiawang/p/14332681.html