接收到tcp / ip响应后,Visual Basic表单冻结 [英] Visual Basic form freezes after receiving tcp/ip response

查看:189
本文介绍了接收到tcp / ip响应后,Visual Basic表单冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Linux主机上的Python和Windows主机上的Visual Basic之间建立了基本的TCP / IP通信设置。 Windows主机似乎工作正常,作为一个测试,我发送一个0到Linux机器,并让它响应打印到Visual Basic调试控制台中的一个0。所有工作正常,但Visual Basic接收到响应并成功显示后,冻结了表单,因此我无法按另一个按钮。这是一个代码示例。

  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)
昏暗networkStream作为NetworkStream = tcpClient.GetStream()
如果networkStream.CanWrite和networkStream.CanRead然后
'做一个简单的写。
SendBytes As [Byte]()= Encoding.ASCII.GetBytes(0)
networkStream.Write(sendBytes,0,sendBytes.Length)
'将NetworkStream读入一个字节缓冲。
Dim bytes(tcpClient.ReceiveBufferSize)As Byte
networkStream.Read(bytes,0,CInt(tcpClient.ReceiveBufferSize))
'将从主机接收到的数据输出到控制台。
Dim returndata As String = Encoding.ASCII.GetString(bytes)
Console.WriteLine((Host returned:+ returndata))
tcpClient.Close()
Else
如果不是networkStream.CanRead然后
Console.WriteLine(无法将数据写入此流)
tcpClient.Close()
否则
如果不是networkStream.CanWrite然后
Console.WriteLine(无法从此流读取数据)
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


解决方案

由于OP请求,我将我的评论提升为我答复帖子的一部分^^

请参阅多线程和Console.WriteLine



许多人使用Console.WriteLine作为登录多线程程序。但实际上,这会让事情变得缓慢。 控制台I / O流已同步,即阻止I / O操作。无论何时多个线程使用Console.WriteLine,只有一个线程可以执行I / O操作,而其他线程需要等待


$ b


原始文章



我想知道您需要执行使用Visual Basic .NET进行多线程编程。因为你在主线程(UI线程)中拥有TCP客户端活动,所以你不能在UI上做任何事情,比如按钮点击,除非TCP客户端活动



我的建议是将TCP客户端活动放入一个函数中,并在单击button1后启动另一个线程继续执行它。

  Sub Tcpclient()
'TCPClient函数的语句
End Sub

Private Sub Button1_Click(sender As Object,e As EventArgs)处理Button1.Click
Dim tcpClientThread As New System.Threading.Thread(_
AddressOf Tcpclient)
tcpClientThread.Start()
End Sub


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   

解决方案

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

这篇关于接收到tcp / ip响应后,Visual Basic表单冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆