unit VitalChecker;

interface

uses
  Windows, Classes, SysUtils;

const
  DEFAULT_INTERVAL = 1000;

type
 TVitalChecker = class
 private
   FOldTick : Cardinal;
 private
    FInterval: integer;
    FActive: boolean;
    procedure SetActive(const Value: boolean);
    function GetIsAlive: boolean;
 public
   constructor Create;

   procedure KeepAlive;

   property Active : boolean read FActive write SetActive;
   property Interval : integer read FInterval write FInterval;
   property IsAlive : boolean read GetIsAlive;
 end;

implementation

{ TVitalChecker }

constructor TVitalChecker.Create;
begin
  inherited;

  FActive := false;
  FInterval := DEFAULT_INTERVAL;
end;

function TVitalChecker.GetIsAlive: boolean;
var
  Tick : Cardinal;
begin
  Result := false;
  if not FActive then Exit;
  if FOldTick = 0 then Exit;

  Tick := GetTickCount;

  Result := (Tick < FOldTick) or ((Tick-FOldTick) <= FInterval);
end;

procedure TVitalChecker.KeepAlive;
begin
  FOldTick := GetTickCount;
  if FOldTick = 0 then FOldTick := 1;
end;

procedure TVitalChecker.SetActive(const Value: boolean);
begin
  FActive := Value;
  FOldTick := 0;
end;

end.
