Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   TTimer zu schnell? (https://www.delphipraxis.net/215491-ttimer-zu-schnell.html)

Kas Ob. 16. Jul 2024 10:42

AW: TTimer zu schnell?
 
Keep it simple and readable like this
Code:
procedure TMainForm.FormCreate(Sender: TObject);
begin
  FAppStartTick := GetTickCount;    // or even better GetTickCount64
  FAppStartTime := Now;
end;

procedure TMainForm.Timer1Timer(Sender: TObject);
const
  ALLOWED_DATETIME_DEVIATION_SEC = 60;
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);

  if LDeltaSeconds > ALLOWED_DATETIME_DEVIATION_SEC then
  begin
    raise Exception.Create('System Time has been changed form the expected time by '+IntToStr(LDeltaSeconds)+' seconds');
  end;
end;
This has nothing to do with timer interval nor will depend on any OS specific timing functionality that might comes slow or fast (like timers messages or Sleep in Background Thread...)
It will work as intended always.

TurboMagic 16. Jul 2024 12:01

AW: TTimer zu schnell?
 
Hallo,

danke für diesen Codevorschlag. Werde diesen sobald wie möglich mal
umsetzen und den Anwender testen lassen.

Kas Ob. 16. Jul 2024 12:57

AW: TTimer zu schnell?
 
Zitat:

Zitat von TurboMagic (Beitrag 1538927)
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;


Alle Zeitangaben in WEZ +1. Es ist jetzt 21:52 Uhr.
Seite 2 von 2     12   

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz