Socket.Receive打开UI无响应 [英] Socket.Receive turn UI unresponsive

查看:66
本文介绍了Socket.Receive打开UI无响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

每次我尝试从套接字接收数据时,我的应用程序都会变得无响应,已经尝试使用线程,bgworkers和委托,但是我的应用程序仍然无响应.

有办法解决吗?

Hello all,

Every time i''ve tried to receive data from a socket my app becomes unresponsive, already tried use thread''s, bgworkers and delegates but my app still unresponsive.

There''s is a way to solve this?

Private Sub ReceiveData()
        Dim buffer(800) As Byte
        If Me.InvokeRequired Then
            Me.Invoke(New MethodInvoker(AddressOf ReceiveData))
        End If
        Application.DoEvents()
        If socket.Connected Then
            Me.txtLogCliente.Text += "Recebendo..." & vbCrLf
            socket.Receive(buffer)
            Interpretador(Encoding.ASCII.GetString(buffer))
            Me.txtLogCliente.Text += Encoding.ASCII.GetString(buffer) & vbCrLf                        
        End If
   End Sub

推荐答案

您的代码确保该方法在UI线程上被调用,这将阻止任何其他处理.此外,它的操作也有误(如果它是非UI线程,则将首先在UI线程上调用它,然后将其返回并在非UI线程上再次调用).

查看Thread.Start.一旦获得文本结果,然后调用UI线程以将结果显示到文本框中.
Your code ensures the method is called on the UI thread, which will block any other processing. Also, it does so incorrectly (if it is a non-UI thread, it will first be invoked on the UI thread, then it will return and be invoked again on the non-UI thread).

Look into Thread.Start. Once you get the text result, THEN invoke a UI thread to display the result to the textbox.


这解决了我的问题!

http://www.codeproject.com/KB/IP/Silence_Socket.aspx
This solved my problem!

http://www.codeproject.com/KB/IP/Silence_Socket.aspx


这篇关于Socket.Receive打开UI无响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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