I have been trying to process some text about 10000 lines to upwards of 26000 lines of text. I wrote a program to search for text strings and copy the lines around them if they are found.
It searches Line by Line.
I need to improve this code speed so it doesn't take so long.
What I think is happening is when I ask to copy line 1 it copies line 1 then when I want to copy line 2 it reads line 1 then copies line 2. for line 3 copy it reads 1,2 then copies 3. This is killing my buffer and significantly slowing down the process.
Now I could be wrong but I think this is what is going on and I have no Idea how to fix it. I know its possible to read the entire file to memory, but I don't think I can pull search strings line by line against the text when in memory.
I would like to utilize a Find Feature if possible to search the entire document and store the required line numbers in a ListBox. Then use the line numbers from that listbox to copy the lines found plus a few around them (it would need to store lines found + extras in the list box first)
Other possible helpfuls... add memory allocation, or keeping the buffer from Re Reading Line by Line every Single Time it searches a single line of text.
Any Help would be Greatly Appreciated ... Code below
Private Sub Strip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Strip.Click Dim theStringBuilder As New System.Text.StringBuilder() RichTextBox1.Text = String.Empty MsgBox("THIS MAY TAKE SOME TIME - MAY EVEN SHOW NOT RESPONDING - JUST WAIT - YOU CAN CONTINUE TO WORK WHILE THIS IS RUNNING", vbOKCancel) RichTextBox1.Text &= "(THIS DOCUMENT HAS BEEN MODIFIED WITH STRIP-N-SAVE)" & vbNewLine 'Correcting Blank Values on Form If TextBox5.Text = "" Then TextBox5.Text = "75" End If If TextBox4.Text = "" Then TextBox4.Text = "20" End If If TextBox3.Text = "" Then TextBox3.Text = "20" End If If TextBox1.Text = "" Then TextBox1.Text = "M6" End If If TextBox2.Text = "" Then TextBox2.Text = TextBox1.Text End If 'Variables Dim endLine = CInt(TextBox5.Text) Dim LineCount As Integer = Document.Lines.Length Dim randarray(0 To LineCount) As Long ProgressBar1.Maximum = UBound(randarray) Dim StartLine = CInt(TextBox3.Text) Dim Ending = CInt(TextBox4.Text) Dim J, L, kk As Integer 'Correcting Input if Document too Short If LineCount < endLine - 1 Then endLine = LineCount End If 'Copying First Lines At all times For x As Integer = 0 To endLine - 1 RichTextBox1.Text &= Document.Lines(x) & vbNewLine J = x Next 'Search For Text and Copy if Exists For x As Integer = endLine To LineCount - 1 If Document.Lines(x).Contains(TextBox1.Text) Or Document.Lines(x).Contains(TextBox2.Text) Then If LineCount <= x + Ending Then Ending = 10 End If If LineCount <= x + Ending Then Ending = 5 End If If LineCount <= x + Ending Then Ending = 1 End If If LineCount <= x + Ending Then Ending = 0 End If If LineCount <= x + Ending Then Exit For End If L = x - StartLine kk = L If kk <= J + StartLine Then L = J + 1 End If For k As Integer = L To x + Ending RichTextBox1.Text &= Document.Lines(k) & vbNewLine If LineCount <= k Then Exit For End If Next x = x + Ending J = x ProgressBar1.Value = x 'Stop if Document is Too Short If x >= LineCount - 1 Then Exit For End If Else If x >= LineCount - 1 Then Exit For End If End If 'Stop if Document is Too Short If x >= LineCount - 1 Then Exit For End If Next ProgressBar1.Value = LineCount Try My.Computer.Audio.Play("C:\WINDOWS\Media\notify.wav") Catch ex As Exception End Try MsgBox("Document Complete - Press Save", vbOKCancel) End Sub
--- NOTE --- Document.Text is a RichTextBox