mainform部分代码
procedure TMain.GetThread;
var
i: integer;
start: array[1..100] of integer;
last: array[1..100] of integer; //改用了数组,也可不用
fileName: string;
begin
fileName := aFile;
MyThread[i]:= TDownLoad.Create(ListView1,aURL, aFile);
tcount:=tcount+1;
i := i + 1;
sleep(10);
end;myurl:=url;
aurl:=url;
myurl1:=geturl(myurl);
myurl:=geturl(myurl);
aFile := GetURLFileName(aURL); //得到文件名if myurl='exe' then
begin
if (checkurl0(aFile)=1) or(checkurl0(aFile)>1) then
begin
tcount := 0;
aURL := aurl; //下载地址
aFile:=aFile;
j := 1;
aFileSize := GetFileSize(aURL);
avg := trunc(aFileSize / nn);
lcount:=ListView1.Items.Count;
EaPopup1.PopUp('公告',aFile+'已添加',3);
try
GetThread();
MyThread[j].Resume; //唤醒线程
j := j + 1;
except
Showmessage('创建线程失败!');
Exit;
end;
end
else
begin
EaPopup1.PopUp('公告','文件'+aFile+'重复',3);
exit;
end;
end;
{thead部分代码}constructor TDownLoad.Create(const AListView: TListView;aURL, aFile: string);
begin FListView := AListView;
FreeOnTerminate := true; tURL := aURL;
tFile := aFile; with AListView.Items.Add do
begin
Caption:='';
SubItems.Add(tFile);
SubItems.Add(tURL);
SubItems.Add('未知');
SubItems.Add('0KB');
SubItems.Add('未知');
SubItems.Add('0%');
SubItems.Add('未知'); Data := Self;
end; inherited create(true);
end;procedure TDownLoad.Execute;
var
I: Integer;
begin
DownLodeFile;
end;
procedure TDownLoad.DownLodeFile;
var filepath:string;
tStream: TFileStream;
myidhttp:TIdHTTP;
begin
temhttp := TIdHTTP.Create(nil);
temhttp.onWorkBegin := main.idhttp1WorkBegin;
temhttp.HandleRedirects :=true;
temhttp.onwork := main.idhttp1Work;
temhttp.onStatus := main.idhttp1Status;
main.IdAntiFreeze1.OnlyWhenIdle := False; //设置使程序有反应.
filepath:=ExtractFilePath(ParamStr(0));
if FileExists(filepath+'\DownLoadFiles\'+temFileName) then //如果文件已经存在
begin
tStream := TFileStream.Create(filepath+'\DownLoadFiles\'+tFile, fmOpenWrite);
end
else
begin
tStream := TFileStream.Create(filepath+'\DownLoadFiles\'+tFile, fmCreate);
end; try
temhttp.Get(tURL, tStream); //开始下载
except
main.StatusBar1.Panels[0].Text:=tFile+'下载出错...';
tStream.Free;
Exit;
end;
freeandnil(tstream);
temhttp.Disconnect;
end;
procedure TMain.GetThread;
var
i: integer;
start: array[1..100] of integer;
last: array[1..100] of integer; //改用了数组,也可不用
fileName: string;
begin
fileName := aFile;
MyThread[i]:= TDownLoad.Create(ListView1,aURL, aFile);
tcount:=tcount+1;
i := i + 1;
sleep(10);
end;myurl:=url;
aurl:=url;
myurl1:=geturl(myurl);
myurl:=geturl(myurl);
aFile := GetURLFileName(aURL); //得到文件名if myurl='exe' then
begin
if (checkurl0(aFile)=1) or(checkurl0(aFile)>1) then
begin
tcount := 0;
aURL := aurl; //下载地址
aFile:=aFile;
j := 1;
aFileSize := GetFileSize(aURL);
avg := trunc(aFileSize / nn);
lcount:=ListView1.Items.Count;
EaPopup1.PopUp('公告',aFile+'已添加',3);
try
GetThread();
MyThread[j].Resume; //唤醒线程
j := j + 1;
except
Showmessage('创建线程失败!');
Exit;
end;
end
else
begin
EaPopup1.PopUp('公告','文件'+aFile+'重复',3);
exit;
end;
end;
{thead部分代码}constructor TDownLoad.Create(const AListView: TListView;aURL, aFile: string);
begin FListView := AListView;
FreeOnTerminate := true; tURL := aURL;
tFile := aFile; with AListView.Items.Add do
begin
Caption:='';
SubItems.Add(tFile);
SubItems.Add(tURL);
SubItems.Add('未知');
SubItems.Add('0KB');
SubItems.Add('未知');
SubItems.Add('0%');
SubItems.Add('未知'); Data := Self;
end; inherited create(true);
end;procedure TDownLoad.Execute;
var
I: Integer;
begin
DownLodeFile;
end;
procedure TDownLoad.DownLodeFile;
var filepath:string;
tStream: TFileStream;
myidhttp:TIdHTTP;
begin
temhttp := TIdHTTP.Create(nil);
temhttp.onWorkBegin := main.idhttp1WorkBegin;
temhttp.HandleRedirects :=true;
temhttp.onwork := main.idhttp1Work;
temhttp.onStatus := main.idhttp1Status;
main.IdAntiFreeze1.OnlyWhenIdle := False; //设置使程序有反应.
filepath:=ExtractFilePath(ParamStr(0));
if FileExists(filepath+'\DownLoadFiles\'+temFileName) then //如果文件已经存在
begin
tStream := TFileStream.Create(filepath+'\DownLoadFiles\'+tFile, fmOpenWrite);
end
else
begin
tStream := TFileStream.Create(filepath+'\DownLoadFiles\'+tFile, fmCreate);
end; try
temhttp.Get(tURL, tStream); //开始下载
except
main.StatusBar1.Panels[0].Text:=tFile+'下载出错...';
tStream.Free;
Exit;
end;
freeandnil(tstream);
temhttp.Disconnect;
end;
解决方案 »
- 简单出错问题
- Winsock2怎么得到本地端口
- 资源文件中对话框整合技术求解
- SQL语句的筛选问题,高分求解???
- 一个最简单的问题!
- 给一个大专院校做学生管理系统,给熟人做的,做了差不多2个月,要6K过不过分?
- 记录POST时出现异常:无法为更新行集定位:一些值可能已在最后读取后改变
- 我发布了一个小软件~~http://expert.csdn.net/cnshare/soft/12/12362.shtm
- 字体乱码的问题
- 我看了一篇关于第三方控件的评测,说一个叫mwEdit的Editor类控件很好,谁知道在哪里可以下载?
- fastreport3.0 简单问题,为什么第一条记录显示不出来?
- 请问delphi5编译的dll能不能在delphi6下调用?
在多个线程工作的时候,在idhttp的onwork里面返回信息的时候,应该是在listview里面一行显示一个,自己的同步信息,可是现在在我这里的表现是,多线程工作是没有问题,但是所有的返回信息总是在最后一个listview行里面显示,请问如何能拆开显示
Synchronize causes the call specified by Method to be executed using the main thread, thereby avoiding multi-thread conflicts. If you are unsure whether a method call is thread-safe, call it from within the Synchronize method to ensure that it executes in the main thread.
我晕难道就没有人愿意帮我看看吗?求救啊!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
你现在的效果应该是这样:
----------------ListView-----------------
线程 1 xxxxxxxxxxxxxxxxxxxxxx
线程 1 xxxxxxxxxxxxxxxxxxxxxx
线程 2 xxxxxxxxxxxxxxxxxxxxxx
线程 2 xxxxxxxxxxxxxxxxxxxxxx
线程 1 xxxxxxxxxxxxxxxxxxxxxx
线程 2 xxxxxxxxxxxxxxxxxxxxxx
-----------------------------------------
你是不是想这样? 还是我理解的有问题,你只用 1 ListView 还是你有多个 ListView?
----------------ListView-----------------
线程 1 xxxxxxxxxxxxxxxxxxxxxx
线程 1 xxxxxxxxxxxxxxxxxxxxxx
线程 1 xxxxxxxxxxxxxxxxxxxxxx
线程 2 xxxxxxxxxxxxxxxxxxxxxx
线程 2 xxxxxxxxxxxxxxxxxxxxxx
线程 2 xxxxxxxxxxxxxxxxxxxxxx
-----------------------------------------
我已经解决了虽然你也没帮上我不过还是谢谢你