H
hongzenghuang
Unregistered / Unconfirmed
GUEST, unregistred user!
type mythread=array[1..10] of DLthread;
//一个线程数组类型
//多线程的产生
function tform2.getthread(aurl,localfile:string;count:integer):mythread;
var
i ,start,left,size,blocksize: integer;
FileName : String;
mydlthread:mythread;
begin
i:=1;
filename:=geturlfilename(aurl);//geturlfilename从一个url中提取要下载文件名
size:=getfilesize(aurl);//getfilesize返回文件大小
blocksize:=size div count;//count是线程数
while i<=countdo
begin
if i<count then
begin
start:=blocksize*(i-1);
left:=blocksize;
end else
begin
start:=blocksize*(count-1);
left:=size-start;
end;
MydlThread.create(aURL,start,left,localfile);
//运行到这里出现access violation at address的错误 请指教
i :=i+1;
end;
result:= mydlthread;
end;
procedure TForm2.Button1Click(Sender: TObject);//下载按钮
var
i,count:integer;
localfile,aurl:string;
mydlthread:mythread;
begin
aURL := Edit1.Text;
//ftp方式下载地址
localfile:=edit2.Text;
count:= StrToInt(combobox1.Text);
//输入的线程数
i:=1;
try
mydlthread:=GetThread(aurl,localfile,count);
while i<=countdo
begin
MydlThread.Resume;
//唤醒线程
i:=i+1;
end;
except
Showmessage('创建线程失败!');
Exit;
end;
end;
constructor DLthread.create(aURL: String;astart,aleft:integer;afilename:string);
begin
turl:=aURL;
tstart:=astart;
tleft:=aleft;
tfilename:=afilename;
end;
//下面downloadfile过程有些地方自己搞不懂 我一个下载线程有tstart(下载起点) tleft(此线程下载字节数),url,localfile(本地文件名) 我现在还不知道怎么判断线程已经下载完它要下载的字节数
procedure DLthread.downloadfile;
var
IdFTP1: TIdFTP;
tstream:TFileStream;
name:string;
begin
Idftp1:=TIdFTP.create(nil);
name:=form2.geturlfilename(turl);
if FileExists(tfilename) then
tStream:=TFileStream.create(tfilename,fmOpenWrite) else
tStream:=TFileStream.create(tfilename,fmcreate);
idftp1.SendCmd('rest '+inttostr(tstart),-1);
tstream.Seek(tstart,sofrombegin
ning);
try
idftp1.Get(name,tstream,true);
finally
tstream.Free;
end;
end;
procedure DLthread.Execute;
begin
{ Place thread code here }
synchronize(DownLoadFile);
end;
//一个线程数组类型
//多线程的产生
function tform2.getthread(aurl,localfile:string;count:integer):mythread;
var
i ,start,left,size,blocksize: integer;
FileName : String;
mydlthread:mythread;
begin
i:=1;
filename:=geturlfilename(aurl);//geturlfilename从一个url中提取要下载文件名
size:=getfilesize(aurl);//getfilesize返回文件大小
blocksize:=size div count;//count是线程数
while i<=countdo
begin
if i<count then
begin
start:=blocksize*(i-1);
left:=blocksize;
end else
begin
start:=blocksize*(count-1);
left:=size-start;
end;
MydlThread.create(aURL,start,left,localfile);
//运行到这里出现access violation at address的错误 请指教
i :=i+1;
end;
result:= mydlthread;
end;
procedure TForm2.Button1Click(Sender: TObject);//下载按钮
var
i,count:integer;
localfile,aurl:string;
mydlthread:mythread;
begin
aURL := Edit1.Text;
//ftp方式下载地址
localfile:=edit2.Text;
count:= StrToInt(combobox1.Text);
//输入的线程数
i:=1;
try
mydlthread:=GetThread(aurl,localfile,count);
while i<=countdo
begin
MydlThread.Resume;
//唤醒线程
i:=i+1;
end;
except
Showmessage('创建线程失败!');
Exit;
end;
end;
constructor DLthread.create(aURL: String;astart,aleft:integer;afilename:string);
begin
turl:=aURL;
tstart:=astart;
tleft:=aleft;
tfilename:=afilename;
end;
//下面downloadfile过程有些地方自己搞不懂 我一个下载线程有tstart(下载起点) tleft(此线程下载字节数),url,localfile(本地文件名) 我现在还不知道怎么判断线程已经下载完它要下载的字节数
procedure DLthread.downloadfile;
var
IdFTP1: TIdFTP;
tstream:TFileStream;
name:string;
begin
Idftp1:=TIdFTP.create(nil);
name:=form2.geturlfilename(turl);
if FileExists(tfilename) then
tStream:=TFileStream.create(tfilename,fmOpenWrite) else
tStream:=TFileStream.create(tfilename,fmcreate);
idftp1.SendCmd('rest '+inttostr(tstart),-1);
tstream.Seek(tstart,sofrombegin
ning);
try
idftp1.Get(name,tstream,true);
finally
tstream.Free;
end;
end;
procedure DLthread.Execute;
begin
{ Place thread code here }
synchronize(DownLoadFile);
end;