0

I have a code for Visual Basic programming language that reads byte array from files, I use that code:

Imports System.IO
Imports System.Threading
Public Class Form1
     Dim thread As New Thread(AddressOf RW_EXE)
     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
         With OpenFileDialog1
             If .ShowDialog() = Windows.Forms.DialogResult.OK Then
                  thread.IsBackground = True
                  Control.CheckForIllegalCrossThreadCalls = False
                  thread.Start()
             End If
         End With
     End Sub
     Sub RW_EXE()
         RichTextBox1.Text = ""
         Dim FS As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
         Dim BS As New BinaryReader(FS)
         Dim x As Integer = BS.BaseStream.Position
         Dim y As Integer = BS.BaseStream.Length
         Dim s As String = ""
         ProgressBar3.Maximum = y
         While x < y
             RichTextBox1.Text &= BS.ReadByte.ToString("X") & " "
             ProgressBar3.Value = x
             x += 10
         End While
         RichTextBox1.Text = s
         FS.Close()
         BS.Close()
         thread.Abort()
     End Sub

That code does it's job well, but I have one problem, It's very slow, it takes big time to read array bytes from files with size of 100 KB and from bigger files.

Please, help.

Thanks for attention.

3
  • You should not be calling controls from inside the Thread. Commented Aug 8, 2015 at 20:14
  • It's slow because of multiple reasons. First, you're reading the file one byte at a time. Second, you're converting every byte to a string. Third, you're updating the RichEditBox.Text with every single byte, which is extremely slow. Fourth, you're updating the ProgressBar with every single byte (with a large file, you're doing many updates that will cause no visible change, which means you're doing it for absolutely no reason). Finally, you're clearing RichEditBox.Text by setting it to an empty string (s), so why put the text in the richtextbox in the first place? Commented Aug 8, 2015 at 21:20
  • Get rid of this line: Control.CheckForIllegalCrossThreadCalls = False and do the threading properly. That line only hides problems. Commented Aug 10, 2015 at 17:34

1 Answer 1

1

When you concatenate to .Text, it will get slower and slower as the text gets bigger because the entire string has to be copied out, appended to (making a new longer string), and then copied back to the RichTextBox.

Instead, use the AppendText() method:

    RichTextBox1.AppendText(BS.ReadByte.ToString("X") & " ")

For the progress, you could instead implement a "timer" into the loop directly and update every xxx milliseconds:

    Dim sw As New Stopwatch
    sw.Start()
    While x < y
        RichTextBox1.AppendText(BS.ReadByte.ToString("X") & " ")
        If sw.ElapsedMilliseconds > 500 Then
            ProgressBar3.Value = x
            sw.Restart()
        End If
        x += 10
    End While

Also, you should disable the button when it's clicked, then re-enable it once all the processing is done.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.