There is too little information from the OP.
What would be the best way to implement a command timeout?
Command to what? External device or the operating system?
I want to repeatedly call a function and if its result doesn't change after 2 seconds then fail?
Call the function with what frequency?
Do the call frequency affects the result, i.e. is the function 'drives' something (on each call) or just fetches a value?
Is it a GUI application?
Is it a multi-threaded application?
How quick the change in the result should be detected?
If it is a GUI application, some delays can be tolerated, with an external process driven, the reaction time can be of high importance.
If the quick reaction is not so important, I'd go for something like:
var
I, OldV, NewV: integer;
begin
OldV := MyFunction();
for I := 1 to 20 do
begin
Sleep(100);
NewV := MyFunction();
if OldV <> NewV then
Break;
end;
if NewV = OldV then
; // Timeout!
end;
This will keep the thread mostly inactive, giving more processor time for the ongoing command/process.
While the previous
Zvoni suggestions are perfectly valid, calling
GetLocalTime (Now) or
GetTickCount64 in a very tight loop can degrade MT and to slow down execution of concurrent process.
It all depends on the particular case, of course.