所以我是一个主要表单,如果客户端已连接,我想隐藏它,所以我创建了一个从另一个类调用的事件,“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
在课程中引发事件的功能:
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,所以我对如何创建委托感到困惑
需要一些帮助,谢谢
答案 0 :(得分: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