我的代码允许用户打开一个文本文件,每行文本文件的内容放在一个数组中,但是当文本文件的内容超过100,000行或更多时,我的程序会冻结。我尝试过backgroundworker,但似乎不支持OpenFileDialog。
它在1,000行或更少的线路上运行良好,但我需要超过100,000行文本。 有没有办法调整它的性能,所以它不会冻结?
这是我的代码:
Dim Stream As System.IO.FileStream
Dim Index As Integer = 0
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "D:\work\base tremble"
openFileDialog1.Filter = "txt files (*.txt)|*.txt"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
Stream = openFileDialog1.OpenFile()
If (Stream IsNot Nothing) Then
Dim sReader As New System.IO.StreamReader(Stream)
Do While sReader.Peek >= 0
ReDim Preserve eArray(Index)
eArray(Index) = sReader.ReadLine
RichTextBox3.Text = eArray(Index)
Index += 1
'Delay(2)
Loop
Label1.Text = "0/" & eArray.Length & ""
End If
Catch Ex As Exception
MessageBox.Show(Ex.Message)
Finally
If (Stream IsNot Nothing) Then
Stream.Close()
End If
End Try
End If
End Sub
答案 0 :(得分:2)
backgroundWorker不应包含任何UI。
您需要做的是:
如果可以,请避免使用redim,这太可怕了。