来自Class模块的交叉线程

时间:2019-06-05 23:51:06

标签: vb.net

我有一个使用命名管道作为侦听器的类模块。它的线程是从UI启动的。当它接收数据时,我想将数据返回到UI进行处理。

我已经尝试了几个Invoke的示例,但是都没有用。最后一条错误消息是需要分配UI窗口的句柄。

以下是frmUDPTest中的内容:

Delegate Sub ReturnDelegate(ByVal text As String)
Public doReturn As ReturnDelegate

Public Sub DisplayReturn(ByVal ServerResponse As String)
    VBL_RETURN.Text = "Server Return: " & ServerResponse
End Sub

以下是名为ClientPipe的类中的NamedPipe接收器:

Public Shared Sub Receive()
    'This is on a separate thread
    'This is the receiver for the Local UDPServer's return to 
    'the requesting client
    'The pipe's name is passed to the Local UDPserver in the request
    'This routine is on a seperate thread and is started prior to any 
    'communications to the local UDPserver.

    Dim RequestBytes(64) As Byte
    Dim RequestByteCount As Integer = 0
    Dim Response As String
    Try
        frmUDPTEST.doReturn = New frmUDPTEST.ReturnDelegate(AddressOf frmUDPTEST.DisplayReturn)
        ClientReceive = New NamedPipeServerStream(ClientPipeName, PipeDirection.In, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous)
        While True
            ClientReceive.WaitForConnection()
            RequestByteCount = ClientReceive.Read(RequestBytes, 0, ByteSize)
            If RequestByteCount > 0 Then
                Response = Encoding.ASCII.GetString(RequestBytes)  'Convert bytes back to string

                '>>>>> Problem occurs here <<<<<
                frmUDPTEST.Invoke(frmUDPTEST.doReturn, New Object() {Response})

            End If
            ClientReceive.Disconnect()
        End While
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

需要UI例程处理的命名管道返回的数据传递给发出请求的客户端。

0 个答案:

没有答案