Reputation: 99
i am creating a program that can wait for computer to connect in my server this code is initially implemented in console, but i want it in GUI form
MsgBox("Server is ready!", MsgBoxStyle.Information, "ChatApp! | Alert")
server = New TcpListener(ipendpoint)
server.Start()
While True
client = server.AcceptTcpClient
Dim c As New Connection
c.stream = client.GetStream
c.streamr = New StreamReader(c.stream)
c.streamw = New StreamWriter(c.stream)
c.hostname = c.streamr.ReadLine
list.Add(c)
FlatListBox1.AddItem(c.hostname)
ListView1.Items.Add(c.hostname & " is connected")
Dim t As New Threading.Thread(AddressOf ListenToConnection)
t.Start(c)
End While
Me.Show()
after adding this to the Form_load but my form is not clickable or else please help me this code work in background while my form is running
EDIT
I found an answer somewhere in google.
' Background work
Task.Factory.StartNew(Function()
' Update UI thread
End Function).ContinueWith(Function(t)
End Function, TaskScheduler.FromCurrentSynchronizationContext())
but I dont know how to merge it to my codes
i got an error after adding this
If Me.InvokeRequired Then
Me.BeginInvoke(Sub() TextBox1.Text = "Done")
End If
Upvotes: 1
Views: 2351
Reputation: 5194
Your form is not clickable because you are locking the UI thread.
You have a While..End While
loop which never exits.
You need to either change this to exit at some point (the UI will be locked while it is running this loop), or move the code off of the UI thread if it needs to run more than once.
There are a number of choices
I'd suggest looking at Tasks and Continuations as you will are taking the result of reading the stream and interacting with the UI thread (I.E. putting the text into a listbox.)
Upvotes: 3