0

所以我是一个主窗体,如果客户端已连接,我想隐藏它,所以我创建了一个从另一个类“Coms.class”调用的事件我认为你不需要该类的所有代码,但如果你只需要说出来。

这是从 Coms 类监听事件的函数

主要形式:

Private Sub sAuthentified(ByVal sender As Coms, ByVal sTemp As String) Handles mComs.sAuthentified
    If (Equals(sender.AES_Decrypt(sTemp), "$%?SuccesS&*(")) Then
        Me.Hide()

        Dim connected As New Connected(mComs)
        connected.ShowDialog()
        connected.Dispose()
    Else
        Console.WriteLine("NON")
    End If
End Sub

在类 coms 中引发事件的函数:

Private Sub Receiving(ByVal iAr As IAsyncResult)
        Dim bReader As BinaryReader
        Dim sReader As StreamReader
        Dim sReaderBuffer(iPacketSize) As Char
        Dim iReaded As Integer

        Try
            SyncLock _client.GetStream
                _client.GetStream.EndRead(iAr)
            End SyncLock
        Catch ex As Exception
            Exit Sub
        End Try

        Select Case bReadByte(0)
            Case enumTags.Connection
                SyncLock _client.GetStream
                    bReader = New BinaryReader(_client.GetStream)
                    iPacketSize = bReader.ReadInt16
                End SyncLock

                RaiseEvent connectionEstablished(Me)
                _client.GetStream.BeginRead(bReadByte, 0, 1, AddressOf Receiving, Nothing)

            Case enumTags.Authentication
                SyncLock _client.GetStream
                    sReader = New StreamReader(_client.GetStream)

                    iReaded = sReader.Read(sReaderBuffer, 0, iPacketSize)
                End SyncLock

                Dim sTemp As New String(sReaderBuffer, 0, iReaded)
                RaiseEvent sAuthentified(Me, sTemp)

                _client.GetStream.BeginRead(bReadByte, 0, 1, AddressOf Receiving, Nothing)
        End Select

    End Sub

因此,当事件引发时,我收到一条错误消息,说他需要一个委托,因为它是从另一个线程调用的。这是有道理的,因为在 Coms 类中,我在回调中对流进行了异步读取,并且在读取中引发了事件,所以我想这是正常的

我的问题是:我无法在函数隐藏上调用 Invoke,所以我对如何创建委托感到困惑

需要一些帮助谢谢

4

1 回答 1

1

您可以使用自己方法的委托调用 BeginInvoke。然后只调用 Hide()。

这还不够,您还必须确保从 UI 线程调用 ShowDialog()。所以只要确保调用了整个 sAuthentified() 方法:

Private Sub sAuthentified(ByVal sender As Coms, ByVal sTemp As String) Handles mComs.sAuthentified
    If Me.InvokeRequired Then
        Me.BeginInvoke(New Action(Of Coms, String)(AddressOf sAuthentified), sender, sTemp)
    Else
        '' Rest of your code here
        ''...
    End If
End Sub
于 2013-11-10T15:41:10.187 回答