I'm having massive troubles understanding threading. Driving me insane.
Could someone please explain why the following won't work.
It has a background thread. I'm trying to get the text different colours which I can get to work if I don't use threading, but when I do it comes up with cross threading.
The part I don't understand is, if I comment out the do --> loop, the textbox is put onto the form. But, if it isn't I get cross threading errors. I don't know why, in my head, I have created the instance of the textbox, and then inputted the text as I require into the textbox and then I invoke the flowlayoutpanel1 to add the textbox control with everything in it.
Private Sub txtboxcreate(ByVal datastring As System.Object, ByRef currenttb As RichTextBox)
Dim count As Integer = 1
Dim tb As New RichTextBox
Name = ("TxtBox" & CStr(counttotal))
With tb
.Name = Name
.Multiline = True
.AutoSize = True
.Width = 250
.ReadOnly = True
.ScrollBars = False
.Tag = 20
.WordWrap = True
.BackColor = Color.Black
AddHandler tb.MouseClick, AddressOf Me.TextBox1_MouseClick
End With
count = (count + counttotal)
Dim linecount As Integer = 0
Dim strreader As New StringReader(datastring)
Dim line As String
updatefrm(tb)
Do
linecount = linecount + 1
line = strreader.ReadLine()
If line.Contains("xred") Then
tb.SelectionColor = Color.Red
End If
If line.Contains("xblk") Then
tb.SelectionColor = Color.White
End If
tb.appendtext(line)
Loop Until linecount = 15
updatefrm(tb)
End Sub
Private Sub updatefrm(ByVal tb As RichTextBox)
If InvokeRequired Then
Invoke(Sub() FlowLayoutPanel1.Controls.Add(tb))
Else : FlowLayoutPanel1.Controls.Add(tb)
End If
End Sub