vb 打开二进制文件太慢

来源:百度知道 编辑:UC知道 时间:2024/09/25 16:38:05
Private Sub Command3_Click()
Dim fillen As Long
Dim Btmp As Byte
Dim s As Byte
Dim gg As Integer

Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
If Err Then GoTo wrongg
Open "C:\Documents and Settings\pcwung\桌面\2405.hex" For Binary As #1
fillen = LOF(1)
For i = 1 To fillen
Get #1, , Btmp

If Len(Hex(Btmp)) = 1 Then
Text1.Text = Text1.Text & "0" & Hex(Btmp) & " "
Text3.Text = Text3.Text & Btmp
Else
Text1.Text = Text1.Text & Hex(Btmp) & " "
End If
If i Mod 16 = 0 Then
Text2.Text = Text2.Text & "00" & Hex(i) & "H:" & vbCrLf
Text1.Text = Text1.Text & vbCrLf
End If
Next i
wrongg: Close #1

End Sub

如上,打开这个文件的时候,很慢!!文件大一点就死机!!怎么搞!!帮下忙。。。很痛苦

在开头写
Dim filedata() As Byte
还有,读入应该是很快的,但是
写文本的速度可就奇慢无比了(Text1,Text2)
所以,如果在循环的时候你不让程序休息一会儿,让别的程序使用CPU,当然就卡的要死。(CPU都被你的程序使用了,别的程序用个头?)
所以就要用到DoEvents语句
DoEvents的作用是让程序休息,给别的程序使用CPU,轮完一圈继续运行。
读入的那段改改,改后如下:
Private Sub Command3_Click()
Dim fillen As Long
Dim filedata() As Byte
Dim s As Byte
Dim gg As Integer

Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
If Err Then GoTo wrongg
Open "C:\Documents and Settings\pcwung\桌面\2405.hex" For Binary As #1
fillen = LOF(1)
Redim filedata(1 To fillen)
Get #1, , filedata
For i = 1 To fillen
If (i Mod 30)=0 Then Doevents
If Len(Hex(filedata(i))) = 1 Then
Text1.Text = Text1.Text & "0" & Hex(filedata(i)) & " "
Text3.Text = Text3.Text & filedata(i)
Else
Text1.Text = Text1.Text & Hex(filedata(i)) & " "