Examples Delphi

Title: MarkTime / TimeOut
Question: Here are two short routines to handle timout situations and the like,
where the focus doesn't lies on a very exact timing then on
easy handling an zero resource consumption.
Answer:
Here are two short routines to handle timout situations an the like,
where the focus doesn't lies on a very exact timing then on
easy handling an zero resource consumption.
A typical use is like that:
MyTimeM := MarkTime(10000); // 10s max time
repeate
// do somenthing usefull
// ...
until TimeOut(MyTimeM) OR
SomeOtherCondition ;
Function MarkTime(MSek: LongInt):LongInt;
Var TM: LongWord;
// generates a Time-Stamp for MSek 1/1000 Seconds
{
IF TM (MaxInt - MSek) //Wraparound after 49,7 Days
Then MarkTime := MSek - (MaxInt - TM)
Else MarkTime := TM + MSek;
}
Begin
TM := GetTickCount; // DWord Arithmetic isn't supported
asm
MOV EAX,TM
ADD EAX,MSek
JC @M1
{ now DWORD handles Wrap-around}
@M1:
MOV Result,EAX
end;
End;
Function TimeOut(TM:LongInt): Boolean;
(* True if Time for TM has expired *)
Var Ticks: DWORD;
{
B := Ticks TM;
IF B AND ((Ticks - TM) 2000000000) Then B := False; //verm. warten auf berlauf
TimeOut := B;
}
Begin
Ticks := GetTickCount;
asm
MOV EAX,Ticks
SUB EAX,TM
JAE @M1
JMP @M2
@M1:
CMP EAX,$80000000 {Wrap-around occured}
JAE @M2
MOV RESULT,TRUE
JMP @M3
@M2:
MOV RESULT,FALSE
@M3:
end;
End;