UI在不同的线程上冻结

时间:2013-11-14 20:52:22

标签: vb.net

我在其他地方尝试过,但没有任何结果。基本上我希望图像显示在图片框中。

我的代码:

Dim ScreenReceiverClient As New TcpClient
Dim ScreenReceiverServer As New TcpListener(ScreenReceiverPort)
Dim ScreenReceiverListening As New Thread(AddressOf ScreenListen)
Dim GetScreen As New Thread(AddressOf ReceiveScreen)
Private Sub ReceiveScreen()
    While ScreenReceiverClient.Connected = True
        Call New Action(AddressOf ChangeImage).BeginInvoke(Nothing, Nothing)
    End While
End Sub
Private Sub ChangeImage()
    Dim bf As New BinaryFormatter
    PictureBox1.Image = bf.Deserialize(ScreenReceiverClient.GetStream)
End Sub
Private Sub ScreenListen()
    While ScreenReceiverClient.Connected = False
        ScreenReceiverServer.Start()
        ScreenReceiverClient = ScreenReceiverServer.AcceptTcpClient
    End While
    GetScreen.Start()
End Sub

应该在UI线程而不是ScreenReceiverListening线程上调用ChangeImage(),但UI只是冻结。请帮帮我!

1 个答案:

答案 0 :(得分:0)

您需要异步调用Method:

http://msdn.microsoft.com/de-de/library/2e08f6yc(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

另一个简单的方法是使用计时器:

 Private Sub ScreenListen()
        While ScreenReceiverClient.Connected = False
            ScreenReceiverServer.Start()
            ScreenReceiverClient = ScreenReceiverServer.AcceptTcpClient
        End While

        t1.Interval = 1
        t1.Start()
    End Sub

    Private WithEvents t1 As Windows.Forms.Timer

    Private Sub t1_Tick(sender As Object, e As EventArgs) Handles t1.Tick
        t1.Stop()
        ReceiveScreen()
    End Sub