如何显示文件复制进度
这是我刚做的复制文件测试程序,每个缓冲设置为5K,传送和显示方面都不会出错.但是我发现传送300M的大文件时,几个label显示控件经常会停止刷新,有时又会刷新一下,那个进度条控件倒是工作的挺好,实时性很高.
我在vb.net下做程序时,用application.doevent()方法可以让屏幕上的控件实时刷新,不知道VB6里有没有类似的方法?我后来改用timer控件定时刷新,就更不行,连进度条都不动.
Dim ReadBuf() As Byte '读缓冲
Dim WriteBuf() As Byte '写缓冲
Dim iFileLen As Long '文件长度
Dim iAlreadySend As Long '已发送字节数
Dim iNoSend As Long '未发送字节数
Dim iArrayLen As Long '缓冲长度
Private Sub Command1_Click()
'读取文件
Open "d:\temp\魔鬼司令.asf " For Binary Access Read As #1
Open "d:\temp\魔鬼司令1.asf " For Binary Access Write As #2
iAlreadySend = 0
Label2.Caption = "已发送: " & CStr(iAlreadySend) '显示已发送
iFileLen = LOF(1)
Label1.Caption = "文件大小: " & CStr(iFileLen) '显示文件大小
iNoSend = iFileLen
Label3.Caption = "未发送: " & CStr(iNoSend) '显示未发送
ProgressBar1.Value = 0 '显示进度条
Label4.Caption = "百分比:0% " '显示进度
iArrayLen = 5000
ReDim ReadBuf(iArrayLen - 1)
Do While iNoSend > 0
If iNoSend < 5000 Then
'数组长度设为未发送字节数
iArrayLen = iNoSend
ReDim ReadBuf(iArrayLen - 1)
End If
Get #1, iAlreadySend + 1, ReadBuf
'写入文件
Put #2, iAlreadySend + 1, ReadBuf
iAlreadySend = iAlreadySend + iArrayLen
Label2.Caption = "已发送: " & CStr(iAlreadySend) '显示已发送
iNoSend = iFileLen - iAlreadySend
Label3.Caption = "未发送: " & CStr(iNoSend) '显示未发送
ProgressBar1.Value = Int((iAlreadySend / iFileLen) * 100) '显示进度条
Label4.Caption = "百分比: " & CStr(Int((iAlreadySend / iFileLen) * 100)) & "% " '显示进度
Loop
Close #1, #2
End Sub
[解决办法]
你在do循环里面加一个DoEvent