Results 1 to 5 of 5

Thread: Form2 freezing up (TCP Chat program)

  1. #1

    Thread Starter
    New Member netcoder1337's Avatar
    Join Date
    Sep 2019
    Posts
    13

    Question Form2 freezing up (TCP Chat program)

    Hello. I am having an issue with a tcp chat project I've been working on.

    Code:
        Function Connected()
            If RX.BaseStream.CanRead = True Then
                Try
                    While RX.BaseStream.CanRead = True
                        Dim RawData As String = RX.ReadLine
                        If RawData.ToUpper = "/MSG" Then
                            Threading.ThreadPool.QueueUserWorkItem(AddressOf MSG1, "Hello World.")
                        Else
                            If RawData.Contains("usermsg:") Then
                                SendUserMsg(RawData.ToString)
                            ElseIf RawData.Contains("#LAUNCH") Then
                                Form2.Visible = True
                            Else
                                RichTextBox1.Text += "Server >> " + RawData + vbNewLine
                                RichTextBox1.SelectionStart = RichTextBox1.Text.Length
                                RichTextBox1.ScrollToCaret()
    
                            End If
                        End If
                    End While
                Catch ex As Exception
                    Client.Close()
                    _isConnected = False
                    RichTextBox1.Text += "Disconnected" & vbNewLine
                    Button2.Enabled = False
                    Button1.Enabled = True
                End Try
            End If
            Return True
        End Function
    When I send the data from the server to the client Form2 opens up just fine for the client, but it's stuck in a loading state and I cannot move or access the form at all.
    In the connection sub I am using
    Code:
    Threading.ThreadPool.QueueUserWorkItem(AddressOf Connected)
    to initiate the sub. I think the threading might be an issue or maybe I need to flush the data? Thanks for the help.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,336

    Re: Form2 freezing up (TCP Chat program)

    You are making Form2 visible on the same thread as you have an endless While loop. You're not going to be able to do anything on another form as long as that While loop is executing. You really need to make sure that nothing that isn't either directly related to the UI or doesn't complete very quickly gets executed on the UI thread. Long-running network communication should be performed on a different thread. The TcpListener, TcpClient, Socket and NetworkStream classes all have asynchronous support baked in. If you haven't already, I suggest that you follow the CodeBank link in my signature below and check out my Asynchronous TCP thread.

  3. #3

    Thread Starter
    New Member netcoder1337's Avatar
    Join Date
    Sep 2019
    Posts
    13

    Re: Form2 freezing up (TCP Chat program)

    Alright, that makes sense. Any tips/examples on how I should go about creating a new function/sub/threading for network communication purposes without freezing up the UI? I appreciate your response as well as your TCP thread, but I'm trying to get this to work within my project without requiring other .dll files in the project or having to re-code it to be compatible with your .dll

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,336

    Re: Form2 freezing up (TCP Chat program)

    If you're going to implement multi-threading/parallelism then you're code is going to have to change significantly. That's just the way it is.

  5. #5

    Thread Starter
    New Member netcoder1337's Avatar
    Join Date
    Sep 2019
    Posts
    13

    Re: Form2 freezing up (TCP Chat program)

    Quote Originally Posted by jmcilhinney View Post
    If you're going to implement multi-threading/parallelism then you're code is going to have to change significantly. That's just the way it is.
    Alright, thanks for the response.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width