1

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

error i got an error after adding this

If Me.InvokeRequired Then
Me.BeginInvoke(Sub() TextBox1.Text = "Done")
End If
something
  • 99
  • 12
  • Put your while loop in place of `Update UI thread` comment. Then _when you are actually_ going to update the UI you need to invoke the update if it's required. These two answers of mine shows examples of invocation: [Answer 1](http://stackoverflow.com/a/35993139/3740093) - [Answer 2](http://stackoverflow.com/a/34768375/3740093) - [Answer 3](http://stackoverflow.com/a/35782000/3740093). – Visual Vincent Mar 15 '16 at 17:17
  • @VisualVincent I know the invocation but how I will invoke the "update UI" – something Mar 16 '16 at 15:02
  • I just gave you three of my answers with examples on how to invoke. What's the problem? – Visual Vincent Mar 16 '16 at 16:07
  • @VisualVincent yes I have red it but i got an error, see my post please – something Mar 16 '16 at 16:42
  • **1)** Use `ContinueWith(Function(t)...`. **2)** The updating should preferrably be inside `StartNew`. **3)** Please post the code too, not just a screenshot. – Visual Vincent Mar 16 '16 at 22:58
  • Sir, I just figure it out. Thankyou for your patience. I try to put it in StartNew. Thankyou so much – something Mar 16 '16 at 23:27

1 Answers1

3

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

  • A background worker
  • A timer
  • A thread,
  • Or Task(of T)

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.)

David Wilson
  • 4,369
  • 3
  • 18
  • 31
NDJ
  • 5,189
  • 1
  • 18
  • 27