我正在执行一个下载数千个文件的长循环。由于可能需要几个小时,因此我希望显示预计剩余时间。但是,根据我编写的代码,我得到的是平均毫秒数。如何将这个从毫秒转换为 TDateTime
?
请参阅我设置 Label1.Caption
的位置:
procedure DoWork;
const
AVG_BASE = 20; //recent files to record for average, could be tweaked
var
Avg: TStringList; //for calculating average
X, Y: Integer; //loop iterators
TS, TE: DWORD; //tick counts
A: Integer; //for calculating average
begin
Avg:= TStringList.Create;
try
for X:= 0 to FilesToDownload.Count - 1 do begin //iterate through downloads
if FStopDownload then Break; //for cancelling
if Avg.Count >= AVG_BASE then //if list count is 20
Avg.Delete(0); //remove the oldest average
TS:= GetTickCount; //get time started
try
DownloadTheFile(X); //actual file download process
finally
TE:= GetTickCount - TS; //get time elapsed
end;
Avg.Add(IntToStr(TE)); //add download time to average list
A:= 0; //reset average to 0
for Y:= 0 to Avg.Count - 1 do //iterate through average list
A:= A + StrToIntDef(Avg[Y], 0); //add to total download time
A:= A div Avg.Count; //divide count to get average download time
Label1.Caption:= IntToStr(A); //<-- How to convert to TDateTime?
end;
finally
Avg.Free;
end;
end;
PS - 我愿意尝试不同的方法来计算最后20个(或AVG_BASE)下载的平均速度,因为我知道我的字符串列表解决方案并不是最好的。我不想基于所有下载来计算它,因为速度可能会在那段时间内发生变化。因此,我只检查最近的20个。