-
Sep 20th, 2019, 08:50 PM
#1
Thread Starter
New Member
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.
-
Sep 20th, 2019, 09:58 PM
#2
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.
-
Sep 21st, 2019, 06:00 PM
#3
Thread Starter
New Member
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
-
Sep 21st, 2019, 10:15 PM
#4
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.
-
Sep 21st, 2019, 10:32 PM
#5
Thread Starter
New Member
Re: Form2 freezing up (TCP Chat program)
Originally Posted by jmcilhinney
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|