67: If Delay procedure
does not work properly, how do I fix it?
A: The Delay
procedure in the Crt unit delays a specified number of
milliseconds. It is declared as "procedure Delay(MS: Word);
". There
are two problems. The procedure requires using the Crt unit and
there is a bug in it in TP 6.0, at least. The alternative is to use
the procedure GetTime(var Hour, Minute, Second, Sec100: Word)
as
shown by the skeleton below
GetTime (...)
initialTime := ...
repeat
GetTime (...)
interval := ... - initialTime;
until interval >= YourDelay;
There are two things you will have to see to. You will have to
convert the time to sec100, and you will have to take care of the
possibility of the interval spanning the midnight. If you do not
wish to program the alternative Delay procedure yourself, you can
use "DOSDELAY Delay without needing the Crt unit"
from TSUNTD.TPU
from ftp://garbo.uwasa.fi/pc/ts/tspa3470.zip.
A2: Dr John Stockton
jrs@dclf.npl.co.uk suggested procedure that is
expanded below. It has the advantage of being concise and
working in
the protected mode. The disadvantage is that it requires a later
TP
version. The solution is quite instructuve.
uses Dos;
{... John's procedure ...}
procedure WAIT (SecondsDelay : real) ;
Var Tptr : ^longint ; Finish : longint ;
begin
Tptr := Ptr(Seg0040, $006C) ;
Finish := Tptr^ + Round(18.2*SecondsDelay) ;
repeat until Tptr^ > Finish ;
end;
{... now let's test it ...}
var h1, m1, s1, sa100 : word;
h2, m2, s2, sb100 : word;
begin
GetTime (h1, m1, s1, sa100);
WAIT (3);
GetTime (h2, m2, s2, sb100);
writeln (h1, ':', m1, ':', s1, '.' ,sa100);
writeln (h2, ':', m2, ':', s2, '.' ,sb100);
end.