Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

GTTimer Thread Based Timer

GTTimer is a Timer Component which delivers the same result as a classic TTimer component but it uses a TThread descentant to implement the logic of the Timer Interval.
This timer is not based on the window messaging system as the classic TTimer component which makes it more reliable and less resource consuming(supposing you have many timers in your application).

There is also a demo of usage at
[link=http://www.gtdelphicomponents.gr/?p=183]GTTimer Demo[/link]
So enough said here is the code



[code]
{ }
{ GT Delphi Components }
{ GT Threaded Timer }
{ }
{ Copyright (c) GT Delphi Components }
{ http://www.gtdelphicomponents.gr }
{ }
{ }

unit o_GTTimer;

interface

uses
Classes
;

type
{------------------------------------------------------------------------------}
TgtTimer = class;
{------------------------------------------------------------------------------}
TgtTimerThread = class(TThread)
private
{ Private declarations }
FTimer : TgtTimer;
protected
{ Protected declarations }
procedure DoTimer;
public
{ Public declarations }
constructor Create(ATimer : TgtTimer);
destructor Destroy;override;
procedure Execute;override;
published
{ Published declarations}
end;
{------------------------------------------------------------------------------}
TgtTimer = class(TComponent)
private
FEnabled: Boolean;
FInterval: Cardinal;
FOnTimer: TNotifyEvent;
procedure SetEnabled(const Value: Boolean);
procedure SetInterval(const Value: Cardinal);
{ Private declarations }
protected
{ Protected declarations }
FTimerThread : TgtTimerThread;
procedure UpdateTimer;
public
{ Public declarations }
constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
published
{ Published declarations}
property Enabled : Boolean read FEnabled write SetEnabled;
property Interval: Cardinal read FInterval write SetInterval;
published
property OnTimer : TNotifyEvent read FOnTimer write FOnTimer;
end;
{------------------------------------------------------------------------------}

implementation
uses
Windows
,SysUtils
;

{ TgtTimerThread }
{------------------------------------------------------------------------------}
constructor TgtTimerThread.Create(ATimer: TgtTimer);
begin
inherited Create(True);
FreeOnTerminate := True;
FTimer := ATimer;
end;
{------------------------------------------------------------------------------}
destructor TgtTimerThread.Destroy;
begin
inherited;
end;
{------------------------------------------------------------------------------}
procedure TgtTimerThread.DoTimer;
begin
if Assigned(FTimer.OnTimer) then
FTimer.OnTimer(FTimer);
end;
{------------------------------------------------------------------------------}
procedure TgtTimerThread.Execute;
begin
while (not Self.Terminated) and (FTimer.Enabled) do
begin
WaitForSingleObject(Self.Handle,FTimer.Interval);
Synchronize(DoTimer);
end;
end;
{------------------------------------------------------------------------------}

{ TgtTimer }
{------------------------------------------------------------------------------}
constructor TgtTimer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FEnabled := True;
FInterval := 1000;
end;
{------------------------------------------------------------------------------}
destructor TgtTimer.Destroy;
begin
inherited;
end;
{------------------------------------------------------------------------------}
procedure TgtTimer.UpdateTimer;
begin
if Assigned(FTimerThread) then
begin
FTimerThread.Terminate;
FTimerThread := nil;
end;
if Enabled then
begin
if FInterval > 0 then
begin
FTimerThread := TgtTimerThread.Create(Self);
FTimerThread.Resume;
end
else
Enabled := False;
end;
end;
{------------------------------------------------------------------------------}

//Getters - Setters\
{------------------------------------------------------------------------------}
procedure TgtTimer.SetEnabled(const Value: Boolean);
begin
FEnabled := Value;
UpdateTimer;
end;
{------------------------------------------------------------------------------}
procedure TgtTimer.SetInterval(const Value: Cardinal);
begin
if Value <> FInterval then
begin
FInterval := Value;
UpdateTimer;
end;
end;
{------------------------------------------------------------------------------}

end.
[/code]
Sign In or Register to comment.