Delphi:我自己的计时器的OnTimer事件永远不会发生 [英] Delphi: OnTimer event of my own Timer never happens

查看:356
本文介绍了Delphi:我自己的计时器的OnTimer事件永远不会发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个no formDelphi单元中的计时器(还有一个表单的主单元),所以我这样做:

I need a Timer in a 'no form' Delphi unit (there's still a main unit with a form), so I do this:

unit ...

interface

type
  TMyTimer = Class(TTimer)
  public
    procedure OnMyTimer(Sender: TObject);
  end;

var
  MyTimer: TMyTimer;

implementation

procedure TMyTimer.OnMyTimer(Sender: TObject);
begin
  ...
end;

initialization

MyTimer := TMyTimer.Create(nil);
with MyTimer do
begin
  Interval := 1000;
  Enabled := True;
  OnTimer := OnMyTimer;
end;

finalization

FreeAndNil(MyTimer);

问题是,OnMyTimer 程序永远不会运行。我真的很感激任何想法,为什么: - )

The problem is that the OnMyTimer procedure is never run. I'll truly appreciate any ideas as to why :-)

推荐答案

除了你创建一个 MyTimer 并释放一个 MouseTimer ,我的代码看不到任何错误(我假设你使用你的代码GUI应用程序或至少具有消息循环)

Apart from the fact you created a MyTimer and freed a MouseTimer, I don't see anything wrong with your code (I do assume you use your code in a GUI application or at least are having a message loop)

此代码示例适用于Delphi 5。 Hello World 每秒都会写入事件日志。

This code sample works with Delphi 5. The Hello Worldget's written to the eventlog every second.

unit Unit2;

interface

uses
  extctrls;

type
  TMyTimer = Class(TTimer)
  public
    procedure OnMyTimer(Sender: TObject);
  end;

var
  MyTimer: TMyTimer;

implementation

uses
  windows, sysutils, classes;

procedure TMyTimer.OnMyTimer(Sender: TObject);
begin
  OutputDebugString(PChar('Hello World'));
end;

initialization

MyTimer := TMyTimer.Create(nil);
with MyTimer do
begin
  Interval := 1000;
  Enabled := True;
  OnTimer := OnMyTimer;
end;

finalization
  FreeAndNil(MyTimer);

end.

这篇关于Delphi:我自己的计时器的OnTimer事件永远不会发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆