在运行时创建并运行TTimer

问题描述 投票:0回答:3

我试图在 Delphi 中实现类似于 Javascript 的

setTimeout()
过程的行为:延迟几秒钟后运行。为此,我在运行时创建一个
TTimer
,运行它,然后释放它。

这是我的代码:

procedure createAndRunTimer();
  procedure goTimer(Sender: TObject);
  begin
    (sender as ttimer).enabled := false;
    // do stuff here
    sender.free;
  end;
var
  t : TTimer;
begin
  t := TTimer.Create(frmprinc);
  t.Interval := 5000;
  t.OnTimer := goTimer(t);
end;

但是我的代码无法编译,编译器返回以下错误:

[DCC错误]unit1.pas(2153):E2010不兼容的类型:'TNotifyEvent'和'过程,无类型指针或无类型参数'”

有什么提示吗?

delphi delphi-7
3个回答
9
投票

TNotifyEvent
声明为:

TNotifyEvent = procedure(Sender: TObject) of object;

of object
使其成为闭包,这是一种特殊类型的方法指针,它带有两个指针——一个指向对象的指针,以及一个指向在该对象上调用的非静态类方法的指针。 因此,您不能将独立函数(当然也不能将嵌套函数)直接分配给
TNotifyEvent
。 这就是编译器抱怨的地方。

因此,您需要声明一个类来包装您的

OnTimer
事件处理程序,例如:

type
  TTimerEvents = class
  public
    procedure goTimer(Sender: TObject);
  end;

procedure TTimerEvents.goTimer(Sender: TObject);
begin
  (Sender as TTimer).Enabled := false;

  // do stuff here

  // NOTE: you cannot destroy the Sender object from here, you must delay
  // the destruction until after this handler exits!  You can post a
  // custom window message via PostMessage() and have the message handler
  // call Sender.Free().  Or, you can use a worker thread to call
  // Sender.Free() via TThread.Synchronize() (or TThread.Queue() in Delphi
  // 8 and later).  Or, in Delphi 10.2 Tokyo and later, you can call
  // Sender.Free() via TThread.ForceQueue().  Or, use whatever other
  // mechanism you want to use to call Sender.Free(), as long as it works
  // asynchronously and calls Sender.Free() in the same thread that
  // constructed the TTimer object ...
end;

var
  events: TTimerEvents;

procedure createAndRunTimer();
var
  t : TTimer;
begin
  t := TTimer.Create(frmprinc);
  t.Interval := 5000;
  t.OnTimer := events.goTimer;
  t.Enabled := True;
end;

initialization
  events := TTimerEvents.Create;
finalization
  events.Free;

或者,您可以使用

class
方法,这样您就不需要包装类的实际实例:

type
  TTimerEvents = class
  public
    class procedure goTimer(Sender: TObject);
  end;

class procedure TTimerEvents.goTimer(Sender: TObject);
begin
  (Sender as TTimer).Enabled := false;

  // do stuff here

  // delay-destroy the Sender as needed ...
end;

procedure createAndRunTimer();
var
  t : TTimer;
begin
  t := TTimer.Create(frmprinc);
  t.Interval := 5000;
  t.OnTimer := TTimerEvents.goTimer;
  t.Enabled := True;
end;

或者,在 Delphi 2006 及更高版本中,您可以使用 类帮助器

type
  TTimerHelper = class helper for TTimer
  public
    procedure goTimer(Sender: TObject);
  end;

procedure TTimerHelper.goTimer(Sender: TObject);
begin
  (Sender as TTimer).Enabled := false;

  // do stuff here

  // delay-destroy the Sender as needed ...
end;

procedure createAndRunTimer();
var
  t : TTimer;
begin
  t := TTimer.Create(frmprinc);
  t.Interval := 5000;
  t.OnTimer := t.goTimer;
  t.Enabled := True;
end;

话虽如此,IS 有一种方法可以使用独立函数而不使用任何类包装器:

procedure goTimer(Self: Pointer; Sender: TObject);
begin
  (Sender as TTimer).Enabled := false;

  // do stuff here

  // delay-destroy the Sender as needed ...
end;

procedure createAndRunTimer();
var
  t : TTimer;
  event : TNotifyEvent;
begin
  t := TTimer.Create(frmprinc);
  t.Interval := 5000;

  TMethod(event).Data := nil; // or whatever you want to pass to the Self parameter...
  TMethod(event).Code := @goTimer;
  t.OnTimer := event;

  t.Enabled := True;
end;

0
投票

事实上,你已经很接近了。该错误消息只是有些误导性。基本上,您需要做的就是在过程调用中省略参数,它就会起作用。下面是一个示例:

procedure TMyClass.createAndRunTimer;
var
  t : TTimer;
begin
  t := TTimer.Create;
  t.Interval := 5000;
  t.OnTimer := goTimer; // omit the (t) here
end;

procedure TMyClass.goTimer(Sender: TObject);
begin
  // do stuff here
end;

-1
投票

我使用了 Torry 的这个过程作为延迟,它在执行时不会锁定线程:

procedure Delay(dwMilliseconds: Longint);
var
  iStart, iStop: DWORD;
begin
  iStart := GetTickCount;
  repeat
    iStop := GetTickCount;
    Application.ProcessMessages;
    Sleep(1);
  until (iStop - iStart) >= dwMilliseconds;
end;
© www.soinside.com 2019 - 2024. All rights reserved.