Existing type of routine for messaging routine in Delphi?

I often find that I am declaring a simple procedure type

  TMessageProc  = procedure(const AMsg: String);

in Delphi. The goal is to allow the transfer of callback procedures to processing functions so that they can update the user interface without knowing about the user interface.

Of course, this should be a general paradigm in Delphi programming. Is there a standard procedure type declaration in one of the commonly used units that I can use to do this? Unfortunately, with the roll-my-own method, I don't quite agree between projects on how I name or declare a type.

+3
source share
4 answers

" Delphi" - ( ). ,

type
  TMessageProc  = procedure(const AMsg: String);

procedure DoSomething(OnProgress: TMessageProc);
begin
//  ...
  if Assigned(OnProgress) then OnProgress('123');
//  ...
end;

Delphi , Delphi :

type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure ShowProgress(const AMsg: String);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

type
  TMessageProc  = procedure(const AMsg: String) of object;  // declare event type

procedure DoSomething(OnProgress: TMessageProc);
begin
//  ...
  if Assigned(OnProgress) then OnProgress('123');   // trigger event
//  ...
end;

procedure TForm1.ShowProgress(const AMsg: String);  // event handler
begin
  Label1.Caption:= AMsg;
  Application.ProcessMessages;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  DoSomething(ShowProgress);
end;

, VCL. , unit.pas

  TGetStrProc = procedure(const S: string) of object;
+2
  • .
  • " " - , , , . , - . - TLLMessageProc.
  • , - , , , , .. , .
+1

classes.pas

{ Standard events }

TNotifyEvent = procedure(Sender: TObject) of object;
TGetStrProc = procedure(const S: string) of object;

.

0

TProc SysUtils.

0

Source: https://habr.com/ru/post/1733818/


All Articles