Posts Tagged ‘timer’

Upon request here is a demo for using the TgtTimer component.
Download the GTTimer Demo

GTTimer Demo Main Screen

GTTimer Demo Main Screen


Here is the source code :

{*******************************************************}
{                                                       }
{       GT Delphi Components                            }
{       GT Threaded Timer Demo                          }
{                                                       }
{       Copyright (c) GT Delphi Components              }
{       http://www.gtdelphicomponents.gr                }
{                                                       }
{                                                       }
{*******************************************************}
unit f_main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,o_GTTimer, StdCtrls, ComCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    Label1: TLabel;
    edtTimerInterval: TEdit;
    UpDown1: TUpDown;
    chkBoxTimerEnabled: TCheckBox;
    btnClose: TButton;
    ProgressBar1: TProgressBar;
    Timer1: TTimer;
    ProgressBar2: TProgressBar;
    Label2: TLabel;
    Edit1: TEdit;
    UpDown2: TUpDown;
    CheckBox1: TCheckBox;
    procedure btnCloseClick(Sender: TObject);
    procedure chkBoxTimerEnabledClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
  private
    { Private declarations }
    FTimer : TgtTimer;
    procedure InternalOnTimer(Sender : TObject);
  public
    { Public declarations }
    constructor Create(AOwner : TComponent);override;
    destructor  Destroy;override;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{GTTimer Usage}

{
 Of course TgtTimer can be installed into the IDE but for the purpose
 of this demo it is created and used in runtime and does not
 require installation into the IDE for the Demo to work
}
{------------------------------------------------------------------------------}
constructor TForm1.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  //Creating the GTTimer
  FTimer         := TgtTimer.Create(Self);
  //Assigning the event
  FTimer.OnTimer := InternalOnTimer;
  FTimer.Enabled := False;
end;
{------------------------------------------------------------------------------}
destructor TForm1.Destroy;
begin
  if Assigned(FTimer) then
    FTimer.Enabled := False;//Setting to false will destroy the thread
  inherited;
end;
{------------------------------------------------------------------------------}
procedure TForm1.btnCloseClick(Sender: TObject);
begin
  Close;
end;
{------------------------------------------------------------------------------}
procedure TForm1.InternalOnTimer(Sender: TObject);
begin
  //Just playing with the progress bar
  if (ProgressBar1.Position + 1) < = ProgressBar1.Max then
    ProgressBar1.Position := ProgressBar1.Position + 1
  else
    ProgressBar1.Position := 0;
end;
{------------------------------------------------------------------------------}
procedure TForm1.chkBoxTimerEnabledClick(Sender: TObject);
begin
  //Setting the interval and enable state
  if UpDown1.Position >=0 then
    FTimer.Interval := UpDown1.Position;
  FTimer.Enabled := TCheckBox(Sender).Checked;
end;
{------------------------------------------------------------------------------}

{TTimer Usage}
{------------------------------------------------------------------------------}
procedure TForm1.Timer1Timer(Sender: TObject);
begin
  //Just playing with the progress bar
  if (ProgressBar2.Position + 1) < = ProgressBar2.Max then
    ProgressBar2.Position := ProgressBar2.Position + 1
  else
    ProgressBar2.Position := 0;
end;
{------------------------------------------------------------------------------}
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
    //Setting the interval and enable state
  if UpDown2.Position >=0 then
    Timer1.Interval := UpDown2.Position;
  Timer1.Enabled := TCheckBox(Sender).Checked;
end;
{------------------------------------------------------------------------------}

end.

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).
For any comments suggestions or anything else please leave a comment on the specific post.
So enough said here is the 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.
A support Vote
  • Top Borland Sites
Site Information
  • http://www.gtdelphicomponents.gr/?feed=rss2
Sponsors
  • http://www.rfsource.gr
Login

UserOnline