Hier noch eine Kanone für Spatzen:
Delphi-Quellcode:
TTimerLabel = class(TThread)
private
FTimeLabel: TLabel; //hier die gewünschte Komponente verwenden
FStop: Boolean;
FDelay: Cardinal;
protected
procedure Execute; override;
procedure ShowTime;
public
constructor Create(const aLabel: TLabel; const aDelay: Cardinal; ThreadPriority: TThreadPriority);
procedure Stop;
property Delay: Cardinal read FDelay write FDelay;
end;
constructor TTimerLabel.Create(const aLabel: TLabel; const aDelay: Cardinal;
ThreadPriority: TThreadPriority);
begin
inherited Create(True);
Priority := ThreadPriority;
FStop := False;
FDelay := aDelay;
FTimeLabel := aLabel;
FreeOnTerminate := True;
Resume;
end;
procedure TTimerLabel.Execute;
begin
inherited;
ShowTime;
end;
procedure TTimerLabel.ShowTime;
begin
while not FStop do
begin
FTimeLabel.Caption := TimeToStr(Now);
Sleep(Delay);
end;
end;
procedure TTimerLabel.Stop;
begin
if not FStop then FStop := True;
end;
Und im verwendeten Proc so aufrufen:
Delphi-Quellcode:
TL: TTimerLabel;
procedure TfrmTest.FormShow(Sender: TObject);
begin
TL := TTimerLabel.Create(lbTime, 10, tpNormal); //10 ist OK, mind. 1 verwenden und max 1000 ;)
end;
procedure TfrmTest.FormClose(Sender: TObject; var Action: TCloseAction);
begin
TL.Stop;
end;
Gruß
René