Search code examples
vb.netformsbuttontcpclientvisual-studio-2015

Visual Basic form freezes after receiving tcp/ip response


I have a basic TCP/IP communication setup between Python on a Linux host and Visual Basic on a windows host. The windows host seems to work fine, as a test, I send a 0 to the Linux machine and have it respond with a 0 printed into the Visual Basic debug console. All works fine but after Visual Basic receives the response and display it successfully, it freezes the form so I can't press another button. Here's an example of the code.

Imports System.Net
Imports System.Net.Sockets
Imports System.Text

Public Class Form1
    Shared Sub Main()
        Dim tcpClient As New System.Net.Sockets.TcpClient()
        tcpClient.Connect("192.168.60.124", 9999)
        Dim networkStream As NetworkStream = tcpClient.GetStream()
        If networkStream.CanWrite And networkStream.CanRead Then
            ' Do a simple write.
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("0")
            networkStream.Write(sendBytes, 0, sendBytes.Length)
            ' Read the NetworkStream into a byte buffer.
            Dim bytes(tcpClient.ReceiveBufferSize) As Byte
            networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
            ' Output the data received from the host to the console.
            Dim returndata As String = Encoding.ASCII.GetString(bytes)
            Console.WriteLine(("Host returned: " + returndata))
            tcpClient.Close()
        Else
            If Not networkStream.CanRead Then
                Console.WriteLine("cannot not write data to this stream")
                tcpClient.Close()
            Else
                If Not networkStream.CanWrite Then
                    Console.WriteLine("cannot read data from this stream")
                    tcpClient.Close()
                End If
            End If
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Main()
    End Sub
End Class   

Solution

  • Since OP request, I moved up my comment as part of my answer post ^^

    Refer to Multi-threading and Console.WriteLine,

    "Many people use Console.WriteLine as logging in multi-threading programs. But actually, it will make things slow. Console I/O Streams are synchronized, i.e. it is blocking I/O operation. Whenever multiple threads use Console.WriteLine, only one thread can do I/O operation and others need to wait."

    I wonder this is why the Console.WriteLine blocks the UI?


    Original Post

    I wonder that you need to do Multithreaded Programming with Visual Basic .NET. Because you having the TCP client activity in your main thread (UI thread). Therefore you cannot do anything on your UI such as button click unless the TCP client activity is done.

    My suggestion is put your TCP client activity into a function and start another thread to proceed it after your button1 is clicked.

    Sub Tcpclient()
        ' The statement of TCPClient function
    End Sub
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim tcpClientThread As New System.Threading.Thread( _
            AddressOf Tcpclient)
        tcpClientThread.Start()
    End Sub