I have tried to implement this MULTI-Threading code but... I do not know how. Since I don't think this is properly related to my code.
--Problem Code--SLOW--
Document. is a RichTextBox that I load with a TextFile just for visual reason.
TextBox1 and TextBox2 are the Strings I'm searching Line by Line for.
StartLine is How many Lines I want to copy above this string
Ending is How many Lines I want to Copy Below this String
LineCount is Total Number of Lines in the Document
EndLine is the 75th line, this is so I don't copy lines I already copied.
CODE
Private Sub Strip_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Strip.Click
RichTextBox1.Text = String.Empty
RichTextBox1.Text &= "(THIS DOCUMENT HAS BEEN MODIFIED WITH STRIP-N-SAVE)" & vbNewLine
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
For x As Integer = endLine To LineCount - 1
If Document.Lines(x).Contains(TextBox1.Text) Or Document.Lines(x).Contains(TextBox2.Text) Then
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
This runs really really slow...
I also tried to change the loops to
CODE
RichTextBox1.Text = String.Join(vbNewLine, Document.Lines, L, x + Ending)
If endLine <> UBound(Document.Lines) Then
J = x + Ending - 1
End If
This will write the lines faster then the other Loop Method but, the writing isn't the big slow down... its the Read using Loops. Also it creates other problems, I want to add a text line to the beginning of the document saying it has been modifed, String.Join doesn't let me use "TEXT".
NEXT...
I attempted to use this but I cannot get the code to read correctly as it throws exceptions for Dim d as New.
I'm guess that document.Text isn't new since I already loaded it using the streamreader, but I'm not sure.
Also I'm tring to get this to output to RichTextBox1, and this doesn't do that.
CODE
dim curLine as string
dim oldLine as string
dim bFlag as boolean
Dim SavedLines As New Stack
dim fs = New FileStream(document.text,FileMode.Open,FileAccess.Read)
Dim d as new StreamReader(fs)
d.BaseStream.Seek(0,SeekOrigin.Begin)
while
d.peek()>-1
if bflag = true then
bflag = false
SavedLines.push(d.readLine())
else
oldLine = curLine
curLine = d.readline()
If curLine.Contains(TextBox1.Text) Or curLine.Contains(TextBox2.Text) Then
bFlag = true
SavedLines.push(oldLine)
SavedLines.push(curLine)
end if
end if
End while
d.close()