0
我的代碼讓用戶打開一個文本文件,並將每行文本文件的內容放入數組中,但是當文本文件的內容超過100,000時,線或更多。我已經嘗試了backgroundworker,但它似乎不支持OpenFileDialog。打開超過100,000行的文本文件時程序死機
它可以在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
是啊這工作感謝凍結停止的建議,但我仍然使用redim保存。我現在閱讀關於StringCollection –
這是一個很好的博客,爲什麼不在循環中使用redim preserve:http://alfredmyersjr.wordpress.com/2009/02/12/you-probably-shouldn%E2%80%99t使用-REDIM -bE - - 保存 - 內 - 一環/ –