IdNotify : How to pass parameteres to function

2019-07-26 22:40发布

问题:

I have code typhoon with lazarus installed after a long strugle I have managed to include the unit IdSync to my project.

How can I pass parameteres to a function that I want to execute in the main thread from TIdNotify ?

回答1:

You have to override the TIdNotify.DoNotify() method, then you can pass whatever parameters you want, eg:

type
  TMyNotify = class(TIdNotify)
  protected
    procedure DoNotify; override;
  end;

procedure TMyNotify.DoNotify;
begin
  SomeFunction(parameters);
end;

.

begin
  ...
  TMyNotify.Create.Notify;
  ...
end;

Presumably, you want the calling thread to specify the parameter values, so just make them members of the class, eg:

type
  TMyNotify = class(TIdNotify)
  protected
    Param1: SomeType;
    Param2: SomeType;
    Param3: SomeType;
    procedure DoNotify; override;
  end;

procedure TMyNotify.DoNotify;
begin
  SomeFunction(Param1, Param2, Param2);
end;

.

begin
  ...
  with TMyNotify.Create do
  begin
    Param1 := ...;
    Param2 := ...;
    Param3 := ...
    Notify;
  end;
  ...
end;


标签: indy lazarus