Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

VB
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))
        '      Dim text As String = datastring
        With tb
            .Name = Name
            '         .Text = text
            .Multiline = True
            .AutoSize = True
            .Width = 250
            .ReadOnly = True
            .ScrollBars = False
            '       .MaxLength = 100
            .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



VB
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
Posted
Comments
Sergey Alexandrovich Kryukov 19-May-14 0:23am    
Not clear what "second method" do you mean. Of course it should not work with out Control.Invoke (or BeginInvoke). Do you understand why? But does it work under Invoke? It should.
—SA
Mendaharin 19-May-14 0:33am    
2nd method is after the do line. To check the text to find what the value is, and then append to the textbox.
I have no idea why I need to invoke a second time though. if the tb and everything to do with it is done within that sub main, and then it's passed through to the updateform, I don't understand why it will create the textbox, but not put the text into the box at the same time.
Mendaharin 19-May-14 2:48am    
G'day Sergey
Quick question. I have figured out how to pass a line at a time through to the textbox after invoking the currenttb. But, I can't figure out how to set the colour selection after it.

Private Sub settbtext(ByVal line As String)
If currenttb.InvokeRequired Then
currenttb.Invoke(New Action(Of String)(AddressOf settbtext), line)
Else : currenttb.AppendText(line)
End If
End Sub

Where abouts in here can I assign the colour I want set to the line when appended?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900