Einzelnen Beitrag anzeigen

Kas Ob.

Registriert seit: 3. Sep 2023
346 Beiträge
 
#11

AW: TTimer zu schnell?

  Alt 16. Jul 2024, 11:42
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.
Kas
  Mit Zitat antworten Zitat