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;  

解决方案 »

  1.   

    问题就是跟标题一样的
    在多个线程工作的时候,在idhttp的onwork里面返回信息的时候,应该是在listview里面一行显示一个,自己的同步信息,可是现在在我这里的表现是,多线程工作是没有问题,但是所有的返回信息总是在最后一个listview行里面显示,请问如何能拆开显示
      

  2.   

    VCL控件不是线程安全的,你需要用Synchronize函数调用。参考:
    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.
      

  3.   

    我现在不是安全不安全的问题,是想知道到底怎么让多个线程各自工作的时候在listview里面各自显示各自的信息不要都在一行交替出现
    我晕难道就没有人愿意帮我看看吗?求救啊!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      

  4.   

    把 main.idhttp1WorkBegin、main.idhttp1Work、main.idhttp1Status 的代码贴出来,你贴的都是和你的标题搭不上边的代码。
      

  5.   

    给楼主的一点建议,通常我是不会在线程的 Execute 中写一些相关的界面代码的,因为 VCL 控件不是线程安全的,所以,我会用 SendMessage 或 线程本身的 Synchronize  方法来处理界面回显,尽量用前者,少用 Synchronize。
      

  6.   

    我明白你的意思,楼主是不是想像 蚂蚁或快车的 ListView 那要,一个 ListView 显示一个线程的全部信息?
    你现在的效果应该是这样:
    ----------------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
    -----------------------------------------
      

  7.   

    谢谢cnmaxu(Max) 
    我已经解决了虽然你也没帮上我不过还是谢谢你