我试图在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;
但我的代码无法编译,IDE会返回以下错误:
[DCC错误] unit1.pas(2153):E2010不兼容的类型:'TNotifyEvent' 和'过程,无类型指针或无类型参数'“
任何提示?
由于
答案 0 :(得分:5)
TNotifyEvent
声明为:
TNotifyEvent = procedure(Sender: TObject) of object;
of object
使它成为闭包,这是一种特殊类型的方法指针,带有2个指针 - 指向对象的指针,以及指向非静态类方法的指针在对象上调用它。因此,您不能将独立函数(当然也不是嵌套函数)直接分配给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及更高版本中,您可以使用class helper:
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;
答案 1 :(得分:-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;