Einzelnen Beitrag anzeigen

Kas Ob.

Registriert seit: 3. Sep 2023
353 Beiträge
 
#13

AW: TTimer zu schnell?

  Alt 16. Jul 2024, 13:57
Hallo,

danke für diesen Codevorschlag. Werde diesen sobald wie möglich mal
umsetzen und den Anwender testen lassen.
In that case then this version is better refactored and more useful
Code:
type
  TForm11 = class(TForm)
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    FAppStartTick: Cardinal;   // or even better GetTickCount64
    FAppStartTime: TDateTime;
    function TimeChangeCheck(Silent: Boolean = False): Boolean;
    procedure TimeChangeReset;
  public
    { Public declarations }
  end;

var
  Form11: TForm11;

implementation

uses
  System.DateUtils;

{$R *.dfm}

procedure TForm11.FormCreate(Sender: TObject);
begin
  TimeChangeReset;
end;

procedure TForm11.Timer1Timer(Sender: TObject);
begin
  TimeChangeCheck;
end;

function TForm11.TimeChangeCheck(Silent: Boolean = False): Boolean;
const
  ALLOWED_DATETIME_DEVIATION_SEC = 2;
var
  LDeltaTicks: Cardinal;                   // in case GettickCount instead of GetTickCount64
  LDeltaSeconds: Integer;
  LExpectedTime: TDateTime;
begin
  LDeltaTicks := GetTickCount - FAppStartTick;
  LExpectedTime := IncMilliSecond(FAppStartTime, LDeltaTicks);
  LDeltaSeconds := SecondsBetween(LExpectedTime, Now);
  Result := LDeltaSeconds > ALLOWED_DATETIME_DEVIATION_SEC;
  if Result and not Silent then
    raise Exception.Create('System Time has been changed form the expected time by ' + IntToStr(LDeltaSeconds) + ' seconds');
end;

procedure TForm11.TimeChangeReset;
begin
  FAppStartTick := GetTickCount;   // or even better GetTickCount64
  FAppStartTime := Now;
end;
Kas
  Mit Zitat antworten Zitat