 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Sümer Cip Guest
|
Posted: Mon Jul 10, 2006 2:40 am Post subject: Download Speed |
|
|
Hi,
I want to calculate the download speed of a file which is being downloaded
using TIdHTTP.Get.
I use OnWork event to calculate the data readed.My code seems to work but,
it is always changing 25 to 8 KBPS, 10 to 25, it is changing very fast.What
I expected is a fixed download speed(Because NetLimiter shows a fixed value
and my calculation alg. always shows different value.).Here is the code:
procedure TDownloadThread.OnWork(ASender: TObject;
AWorkMode: TWorkMode; Const AWorkCount: Integer);
function CalculateSpeedPerSec(curr,pre:Integer;BufLen:Integer):Double;
begin
if curr = pre Then
curr := curr+1;
Result := (BufLen / (curr-pre))*1000;
end;
begin
if Not Assigned(fStream) Then
exit;
if AWorkMode = wmRead then
begin
fCurrentTick := GetTickCount;
VirtualTreeData^.SpeedPerSec :=
CalculateSpeedPerSec(fCurrentTick,fPreviousTick,AWorkCount-fPreviousWorkCount);
VirtualTreeData^.FilePosition := AWorkCount;
if VirtualTreeData^.FileSize <> 0 Then
VirtualTreeData^.DownloadedPercentage := (100 * (AWorkCount)) /
VirtualTreeData^.FileSize;
if ((fCurrentTick-fPreviousTick) >= 1000) and (fStream.Position <> 0)
Then
Synchronize(UpdateGUI);
fPreviousTick := fCurrentTick;
fPreviousWorkCount := AWorkCount;
end;
end; |
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Mon Jul 10, 2006 11:00 pm Post subject: Re: Download Speed |
|
|
"Sümer Cip" <sumerc (AT) softhome (DOT) net> wrote in message
news:44b177ad (AT) newsgroups (DOT) borland.com...
| Quote: | I want to calculate the download speed of a file which is being
downloaded using TIdHTTP.Get. I use OnWork event to calculate
the data readed.My code seems to work but, it is always changing
25 to 8 KBPS, 10 to 25, it is changing very fast.What I expected is
a fixed download speed(Because NetLimiter shows a fixed value
and my calculation alg. always shows different value.).
|
Try this instead (untested):
procedure TDownloadThread.OnWork(ASender: TObject; AWorkMode: TWorkMode;
Const AWorkCount: Integer);
var
Elapsed: DWORD;
NumBytes: Integer;
begin
if Not Assigned(fStream) Then Exit;
if AWorkMode = wmRead then
begin
fCurrentTick := GetTickCount;
Elapsed := GetTickDiff(fPreviousTick, fCurrentTick);
if Elapsed = 0 then Elapsed = 1;
VirtualTreeData^.SpeedPerSec := ((AWorkCount -
fPreviousWorkCount) * 1000) / Elapsed);
VirtualTreeData^.FilePosition := AWorkCount;
if VirtualTreeData^.FileSize <> 0 Then
VirtualTreeData^.DownloadedPercentage := (AWorkCount * 100)
/ VirtualTreeData^.FileSize;
if (Elapsed >= 1000) and (fStream.Position <> 0) Then
Synchronize(UpdateGUI);
fPreviousTick := fCurrentTick;
fPreviousWorkCount := AWorkCount;
end;
end;
Gambit |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|