unit ThdTimer;
interface
uses
Windows, Messages, SysUtils, Classes,
Graphics, Controls, Forms, Dialogs;
type
TThreadedTimer = class;
TTimerThread = class(TThread)
OwnerTimer: TThreadedTimer;
procedure Execute;
override;
end;
TThreadedTimer = class(TComponent)
private
FEnabled: Boolean;
FInterval: DWord;
FOnTimer: TNotifyEvent;
FTimerThread: TTimerThread;
protected
procedure Timer;
dynamic;
public
constructor Create(AOwner: TComponent);
override;
destructor Destroy;
override;
published
property Enabled: Boolean
read FEnabled write FEnabled default True;
property Interval: DWord
read FInterval write FInterval default 1000;
property OnTimer: TNotifyEvent
read FOnTimer write FOnTimer;
end;
procedure Register;
implementation
procedure TTimerThread.Execute;
var
dwTick: DWord;
iTick: Integer;
begin
Priority := tpNormal;
//tpTimeCritical;
repeat
dwTick := GetTickCount();
//Synchronize(OwnerTimer.Timer);
OwnerTimer.Timer;
iTick := OwnerTimer.FInterval - (GetTickCount() - dwTick);
if iTick < 0 then
iTick := 0;
Sleep(iTick);
until Terminated;
end;
constructor TThreadedTimer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FEnabled := False;
FInterval := 1000;
FTimerThread := TTimerThread.Create(False);
FTimerThread.OwnerTimer := Self;
end;
destructor TThreadedTimer.Destroy;
begin
with FTimerThreaddo
begin
Terminate;
WaitFor;
Free;
end;
inherited Destroy;
end;
procedure TThreadedTimer.Timer;
begin
if FEnabled and Assigned(FOnTimer) then
FOnTimer(Self);
end;
procedure Register;
begin
RegisterComponents('Samples', [TThreadedTimer]);
end;
end.
我有这个代码,但我看不明白,一起研究一下吧