本人写一个安装程序,要拷贝文件,有一个函数,但一运行,程序退不出来,什么问题
从来未碰过啊
代码以下:
procedure Tfrmmain.SetupOne(Fromfile,Tofile:string);
 var
  GetPath,SetPath:string;
  getStream,setStream: TFileStream;
  num, n: Integer;
  buf: PByte;
  BufSize,block: Integer;
begin
   GetPath:=Fromfile;
   setPath:=Tofile;
  if not FileExists(getPath) then
  begin
   Erpmsgbox('源文件【'+getPath+'】不存在,可能已丢失,请联系供应商...!',1);
   Exit;
  end;
  Try
    getStream := TFileStream.Create(getPath, fmOpenRead or fmShareExclusive);
    setStream := TFileStream.Create(setPath, fmCreate);    num := getStream.Size;
    setStream.Size := num;
    getStream.Position := 0;
    setStream.Position := 0;    BufSize := num;
    block := BufSize div 100;
    GetMem(buf, BufSize);
    ProgressBar1.Max :=100;
    ProgressBar1.Position := 0;    while num <> 0 do
    begin
      Application.ProcessMessages;
      n := block;
      if n > num then n := num;
      getStream.ReadBuffer(buf^, n);
      setStream.WriteBuffer(buf^, n);
      ProgressBar1.Position := Trunc((1 - num / BufSize)*100);
      Dec(num, n);
    end;
  Finally
    ProgressBar1.Position := 0;  
    getStream.Free;
    setStream.Free;
    FreeMem(buf, BufSize);
  End;
end;

解决方案 »

  1.   

    调试,跟踪while处是否能够正常退出
    确保 Dec(num, n);一定能取到0值吗,如果取到负数,也不会退出循环的
      

  2.   

    经查:while num <> 0 do
      begin
      Application.ProcessMessages;
      n := block;
      if n > num then n := num;
      getStream.ReadBuffer(buf^, n);
      setStream.WriteBuffer(buf^, n);
      ProgressBar1.Position := Trunc((1 - num / BufSize)*100);
      Dec(num, n);
      end;
    这段代码有问题,但不知如何来确定什么时候退出,改为NUM>0 也不行
      

  3.   

    调试跟踪num, n 的值,循环跳不出就是条件达不到
      

  4.   

    while num <> 0 do
    Dec(num, n);这样判断很不好,num每次减n,只有2者相等时才是0,循环才会退出
    自己要实现什么,自己改成合适的
      

  5.   

    如果是原样复制文件不建议自己写代码实现,应该利用windows的API copyfile
    The CopyFile function copies an existing file to a new file. BOOL CopyFile(    LPCTSTR lpExistingFileName, // pointer to name of an existing file 
        LPCTSTR lpNewFileName, // pointer to filename to copy to 
        BOOL bFailIfExists  // flag for operation if file exists 
       );
     ParameterslpExistingFileNamePoints to a null-terminated string that specifies the name of an existing file. lpNewFileNamePoints to a null-terminated string that specifies the name of the new file. bFailIfExistsSpecifies how this operation is to proceed if a file of the same name as that specified by lpNewFileName already exists. If this parameter is TRUE and the new file already exists, the function fails. If this parameter is FALSE and the new file already exists, the function overwrites the existing file and succeeds.  Return ValuesIf the function succeeds, the return value is nonzero.
    If the function fails, the return value is zero. To get extended error information, call GetLastError. ResSecurity attributes for the existing file are not copied to the new file. 
    File attributes (FILE_ATTRIBUTE_*) for the existing file are copied to the new file. For example, if an existing file has the FILE_ATTRIBUTE_READONLY file attribute, a copy created through a call to CopyFile will also have the FILE_ATTRIBUTE_READONLY file attribute. For further information on file attributes, see CreateFile.
      

  6.   

    6楼,我知啊,但我不想做那样,我想做成有【进度条】、【剩余时间】的东东!API太快了,我想慢些!
    因为我写的是商业软件! ---【不是每一滴牛奶都叫‘特伦苏’】,【也不是每一滴牛奶都来自奶牛身上】
      

  7.   

    n := block;
    while num <> 0 do
      begin
      Application.ProcessMessages;
      if n > num then n := num;
      getStream.ReadBuffer(buf^, n);
      setStream.WriteBuffer(buf^, n);
      ProgressBar1.Position := Trunc((1 - num / BufSize)*100);
      Dec(num, n);
    end;如果while里面,每一次都等于blcok了
    你测试的文件大小多少?
      

  8.   

    你确定能这样写?  在你循环里面没看到getStream.Position的赋值,这样getStream.ReadBuffer取的流也一直是前面的部分。block你是动态算的,文件过大能行么?