Click here to Skip to main content
15,886,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
G'day

I've got my program working pretty darn close to how I want for a V1.0.

After discussing with Sergey, I've sat down and "tried" to work out how to write a TCP (Is there anywhere in here that I could put this up just so people could have a look at it and give me ideas on improving it?)

What happens is if I send through a stream of data via a networked computer it works, but if I send it locally (It doesn't matter if I choose the loopback address or the network I.P) it comes up as a cross thread on the form update.

I won't throw the whole load up there, but can anyone see why it may be causing an issue locally, but not from a networked computer?

The updateform sub is where the cross threading occurs.

Again, I'm still learning. Please be constructive.

If I haven't included something, I am willing to include it. If you want the whole thing up, I will put it up.

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
            .Height = linecount * 22

            AddHandler tb.MouseClick, AddressOf Me.TextBox1_MouseClick
        End With
        textboxestime.Add(tb, 0)
        count = (count + counttotal)
        currenttb = tb
        updatefrm(tb)
        append(datastring)
    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 27-Aug-14 0:19am    
The key detail is missing from the information on this problem. If you use Invoke and complain about threading issue, there are more than one threads. What threads; what code is executed in what thread? what is the exception, exactly, in what line?
—SA
Mendaharin 27-Aug-14 1:43am    
To my understanding - I have 2 threads. the tcpthread is a background thread, and the other thread is the form.

But actually - the weird thing is, after a reboot of my computer, it seems to be working....

FYI: just in case - it failed at invoke(sub() flowlayoutpanel1.controls.add(tb)
Below is the code in the flow that it runs including the TCP stuff.

Private Sub main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tcpthread As New System.Threading.Thread(AddressOf tcplisten)
tcpthread.IsBackground = True
tcpthread.Start()
textboxestime = New Dictionary(Of RichTextBox, Integer)
textboxestime.Add(Nothing, 0)
Timer1.Start()

End Sub
Public Sub tcplisten()
_listener = New TcpListener(IPAddress.Any, 22490)
_listener.Start()
While True
Do
Dim client As TcpClient = _listener.AcceptTcpClient

client.ReceiveBufferSize = 1024
Sleep(1000)
Try
If client.Connected = True Then
Text_Encode(client)
End If
client.Close()
client = Nothing

Catch
End Try
Loop
End While
End Sub
Private Sub Text_Encode(ByVal client As TcpClient)
Dim bytes(8096) As Byte
Dim incomingdata As String = Nothing
Dim datastring As String
Dim i As Int32
Dim stream As NetworkStream = client.GetStream()

client.ReceiveBufferSize = 8096
i = stream.Read(bytes, 0, bytes.Length)
datastring = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
Dim sr As New StringReader(datastring)
Do Until sr.ReadLine = Nothing
linecount = linecount + 1
Loop
txtboxcreate(datastring, currenttb)
linecount = 0
End Sub
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
.Height = linecount * 22

AddHandler tb.MouseClick, AddressOf Me.TextBox1_MouseClick
End With
textboxestime.Add(tb, 0)
count = (count + counttotal)
currenttb = tb
updatefrm(tb)
append(datastring)
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

(Edit was a useless comment I had put in, not appropriate wording)

1 solution

You create the Textbox in the function Private Sub txtboxcreate - and here is likely the problem: if the function is called in a different thread than the UI thread, that Textbox is created in that different thread, and will later cause problems when it is accessed from the UI thread...
Make sure that that function is already running in the UI thread!
 
Share this answer
 

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