Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi Beschränkung von GetTickCount für Timeout umgehen (https://www.delphipraxis.net/18932-beschraenkung-von-gettickcount-fuer-timeout-umgehen.html)

Vjay 26. Mär 2004 08:12


Beschränkung von GetTickCount für Timeout umgehen
 
Hi, Ihr habt da 2 schöne Beispiele in eurer Codelibrary. Wollte nur mal zeigen wie ich das gelöst habe. Performance-Counter sind mir zu langsam, da ich timeout-Funktionen auch in schnell rotierenden Schleifen nutze.

Delphi-Quellcode:
function initTimeOut(const msec: cardinal): cardinal;
var
 tickSave: cardinal;
begin
 if msec>0 then
 begin
  repeat // hängt am 50igsten tag evtl etwas, aber besser als absturz etc.
   tickSave:= getTickCount;
   result:= tickSave + msec;
   if result< tickSave then sleep(100);
  until result >= tickSave;
 end
 else result:= 0;
end;

function isTimeOut (const timeVal: cardinal): Boolean;
begin
 if timeVal=0 then result:= false
 else result:= (getTickCount> timeVal);
end;
Aufruf erfolgt dann so:

Delphi-Quellcode:
var
 timeOut: cardinal;
begin
 timeOut:= initTimeOut( 5000);
 repeat {doSomething} until isTimeOut( timeOut);
end;
So wird in der Schleife nur noch eine Vergleichsoperation durchgeführt = fix :). Wenn man initTimeout mit 0 aufruft, geht die Funktion nie TimeOut.

Danke fürs lesen :)

Assarbad 27. Apr 2004 13:02

Re: Beschränkung von GetTickCount für Timeout umgehen
 
Der helle Wahnsinn. Performancetechnisch absoluter Mist, aber ansonsten ...

"nur noch eine Vergleichsoperation durchgeführt = fix" halte ich für ein Gerücht, da dies im Endeffekt immer noch vom Compiler abhängt, solange es nicht in ASM implementiert wird.


Alle Zeitangaben in WEZ +1. Es ist jetzt 21:53 Uhr.

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