代码如下:
procedure Tdjbw.mycopyfile(sourcef,targetf:string);
var
FromF, ToF: file;
NumRead, NumWritten: Integer;
Buf: array[1..2048] of Char;
n:integer;
begin
AssignFile(FromF, sourcef);
Reset(FromF, 1); { Record size = 1 }
AssignFile(ToF,targetf); { Open output file }
Rewrite(ToF, 1); { Record size = 1 }
n:=0;
repeat
BlockRead(FromF, Buf, sizeof(buf), NumRead);
ProgressBar1.Position := sizeof(buf)*n*100 div fileSize(FromF);
application.ProcessMessages;
//显示进度
BlockWrite(ToF, Buf, NumRead, NumWritten);
inc(n);
until (NumRead = 0) or (NumWritten <> NumRead);
MessageBox(Application.Handle, '软盘拷贝完成', '信息', MB_OK or MB_ICONASTERISK);
CloseFile(FromF);
CloseFile(ToF);
end;
但当程序运行到MessageBox的时候,拷贝还在进行
是否因为BlockWrite(ToF, Buf, NumRead, NumWritten);之后,数据还没从内存copy到软盘上就马上执行了后面的代码所致??
怎样解决??
procedure Tdjbw.mycopyfile(sourcef,targetf:string);
var
FromF, ToF: file;
NumRead, NumWritten: Integer;
Buf: array[1..2048] of Char;
n:integer;
begin
AssignFile(FromF, sourcef);
Reset(FromF, 1); { Record size = 1 }
AssignFile(ToF,targetf); { Open output file }
Rewrite(ToF, 1); { Record size = 1 }
n:=0;
repeat
BlockRead(FromF, Buf, sizeof(buf), NumRead);
ProgressBar1.Position := sizeof(buf)*n*100 div fileSize(FromF);
application.ProcessMessages;
//显示进度
BlockWrite(ToF, Buf, NumRead, NumWritten);
inc(n);
until (NumRead = 0) or (NumWritten <> NumRead);
MessageBox(Application.Handle, '软盘拷贝完成', '信息', MB_OK or MB_ICONASTERISK);
CloseFile(FromF);
CloseFile(ToF);
end;
但当程序运行到MessageBox的时候,拷贝还在进行
是否因为BlockWrite(ToF, Buf, NumRead, NumWritten);之后,数据还没从内存copy到软盘上就马上执行了后面的代码所致??
怎样解决??
CloseFile(FromF);
CloseFile(ToF);
之后。————————————————————————————————————
宠辱不惊,看庭前花开花落,去留无意;毁誉由人,望天上云卷云舒,聚散任风。
————————————————————————————————————
ProgressBar1.Position := sizeof(buf)*n*100 div fileSize(FromF);
的时候,拷贝小文件时进度条的偏差还不大
但当拷贝大文件时,进度条Position的位置就出错了
是不是ProgressBar1.Position不该这样赋值?