unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
labTimer: TLabel;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TTimerThread=class(TThread)
private
TimeUse: integer;
protected
procedure Execute;
override;
Procedure ShowTimer;
end;
var
Form1: TForm1;
TimerEvent: Thandle;
implementation
{$R *.DFM}
procedure TTimerThread.ShowTimer;
var
hour ,minute ,second: integer;
begin
dec(TimeUse);
hour:=timeuse div 3600;
minute:=(timeuse mod 3600) div 60;
second:=(timeuse mod 60);
with Form1 do
begin
//dec(TimeUse);
labTimer.Caption := inttostr(hour)+':'+inttostr(minute)+':'+inttostr(second)
end;
end;
procedure TTimerThread.Execute;
begin
while TimeUse>=0 do
begin
WaitForSingleObject(TimerEvent,1000);
//可以精确到毫秒
Synchronize(ShowTimer);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
TimerEvent := CreateEvent(nil,True,False,nil) ;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
TimerThread: TTimerThread;
begin
TimerThread := TTimerThread.Create(true);
TimerThread.TimeUse := 600;
TimerThread.Resume;
end;
end.