今天在看一个代码的时候,发现他大量使用Timer来代替线程做一些事情.
突然想到在Timer触发时间到了的时候,如果上一次的代码还没有执行完会是什么情况.
做了个小测试如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
btn1: TButton;
tmr1: TTimer;
lbl1: TLabel;
lbl2: TLabel;
procedure btn1Click(Sender: TObject);
procedure tmr1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
j: Integer = 0;
implementation
{$R *.dfm}
procedure TForm1.btn1Click(Sender: TObject);
begin
tmr1.Enabled := True;
end;
procedure TForm1.tmr1Timer(Sender: TObject);
var
i: Integer;
begin
i := 0;
Inc(i, 10);
Inc(j, 10);
lbl1.Caption := IntToStr(i);
lbl2.Caption := IntToStr(j);
Sleep(5000);
end;
end.
最后发现,如果Timer中的代码如果没有执行完的话,是不会触发下一次定时事件的.