Newsgroups : Borland : borland.public.delphi.internet.winsock : 2008 Mar : Re: idhttp calculation problem , need help!
| Subject: | Re: idhttp calculation problem , need help! |
| Posted by: | "John Herbster" (herb-sci1_at_sbcglobal.net) |
| Date: | Mon, 10 Mar 2008 17:12:03 |
"inqontrol" <team@odium.to> wrote
>> I suspect that your code might be having problems with using the DWORD ...
> I enabled overflow checking, I also changed integer into DWORD for newtime
> and fLastTime -- unfortunately no success. ...
Inqontrol,
Did you solve your problem? If not here are some hints.
HTH, JohnH
This code uses a TTimer to trigger the checks, a button to start the
simulation and an TEdit to report the results. It does not use
the GetTickCount.
Var idhttp: record fWorkPos, fMaxCount: Int64; end;
fLastNow: double; fLastPos: Int64; fLastRate: double;
SimRate: double;
procedure TForm1.Button5Click(Sender: TObject);
begin
SimRate := 100e3{bytes/sec}*(24*60*60); {bytes/day}
idhttp.fWorkPos := 0;
idhttp.fMaxCount := 1000000; {1e6 bytes}
fLastNow := Now;
Timer1.Interval := 1000;
Timer1.Enabled := true;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var CurPos: Int64; CurNow: TDateTime;
DeltaSecs, CurBytesPerSec, Progress, EstSecsToFinish: double;
begin
// Fake progress for testing.
idhttp.fWorkPos := idhttp.fWorkPos
+ round(SimRate*(1+random-random)/(24*60*60));
// Do regular work.
CurNow := Now; {current seconds time}
DeltaSecs := (CurNow - fLastNow)*(24*60*60);
{ Note that a bump in time for a non-24-hour day can cause a problem.}
CurPos := idhttp.fWorkPos; {downloaded bytes}
If (CurPos >= idhttp.fMaxCount)
then begin
Timer1.Enabled := false;
Edit5.Text := 'Finished';
{ ... other completion work ...}
end
else begin
If (DeltaSecs > 0)
then begin
CurBytesPerSec :=
(idhttp.fWorkPos - fLastPos)/DeltaSecs; {bytes/sec}
Progress :=
idhttp.fWorkPos/idhttp.fMaxCount; {fraction complete}
EstSecsToFinish :=
round(((idhttp.fMaxCount - fLastPos))/CurBytesPerSec);
Edit5.Text :=
Format('%.0F%% %F secs to go',
[100*Progress,EstSecsToFinish]);
end
else begin
CurBytesPerSec := 0/0; {Not-A-Number}
Edit5.Text := 'Error';
end;
fLastPos := CurPos;
fLastNow := CurNow;
fLastRate := CurBytesPerSec;
end;
end;