1

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

我的代码:

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

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

4

1 回答 1

0

您需要异步调用方法:

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
于 2013-11-15T08:41:12.527 回答