Einzelnen Beitrag anzeigen

Der schöne Günther

Registriert seit: 6. Mär 2013
6.111 Beiträge
 
Delphi 10 Seattle Enterprise
 
#3

AW: Auto logout nach Zeit

  Alt 7. Apr 2022, 17:53
Ohne dein genaues Anwendungsszenario zu kennen:

Es gibt unter Windows die Methode GetLastInputInfo(..)

Als Delphi verpackt sieht das bei uns so aus:

Delphi-Quellcode:
uses System.TimeSpan, System.SysUtils, Winapi.Windows;

function TTimes.getSinceLastInput(): TTimeSpan;
var
   lastInput: TLastInputInfo;
   currentTickCount: DWORD;
   millisecondsPassed: Double;
begin
   lastInput := Default(TLastInputInfo);
   lastInput.cbSize := SizeOf(TLastInputInfo);

   Win32Check( GetLastInputInfo(lastInput) );
   currentTickCount := GetTickCount();

   // lastInput was before 49.7 days but by now, 49.7 days have passed
   if (lastInput.dwTime > currentTickCount) then
      begin
         millisecondsPassed :=
            (DWORD.MaxValue - lastInput.dwTime)
            +
            (currentTickCount * 1.0); // cast to float by multiplying to avoid DWORD overflow
         Result := TTimeSpan.FromMilliseconds(millisecondsPassed);
      end
   else
      Result := TTimeSpan.FromMilliseconds(currentTickCount - lastInput.dwTime );
end;
  Mit Zitat antworten Zitat