![]() |
AW: TTimer zu schnell?
Keep it simple and readable like this
Code:
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...)
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; It will work as intended always. |
AW: TTimer zu schnell?
Hallo,
danke für diesen Codevorschlag. Werde diesen sobald wie möglich mal umsetzen und den Anwender testen lassen. |
AW: TTimer zu schnell?
Zitat:
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. |
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